convert a _set.all() to a list

For some reason I can’t seem to convert a _set.all() to a list.

azure = Azure_inventory.objects.get(id=id)
network_interface_data = azure.network_interface_azure_set.all()

# network_interface_data = list(azure.network_interface_azure_set.all())
# network_interface_list = network_interface_data[:]
# for network_interface in network_interface_list:
#     subnet = network_interface.tb_subnet
#     network_interface.subnet_name = subnet.subnet_name
# network_interface_json = serializers.serialize('json', network_interface_list)

network_interface_json = serializers.serialize('json', network_interface_data)

But I want to add the subnets to the same dict as network_interface_data

These are my models :

class Azure_inventory(models.Model):
    # Various fields

class Network_interface_azure(models.Model):
    tb_subnet = models.ForeignKey(Subnet_azure, on_delete=models.CASCADE)
    tb_inv_azure = models.ForeignKey(Azure_inventory, on_delete=models.CASCADE)

class Subnet_azure(models.Model):
    subnet_name = models.CharField(max_length=100, blank=True, null=True)


<class ‘django.db.models.fields.related_descriptors.create_reverse_many_to_one_manager..RelatedManager’>

And what does:



<class 'django.db.models.query.QuerySet'>

And so finally, what does:


(Sorry - meant to get that in the prior response - didn’t mean to make this two responses.)

Basic need is to append the data from another table to same QuerySet or List of the first query.

Within the query itself? That doesn’t make sense.

Within the context of a serializer, see Serializers - Django REST framework (Django’s native serializers aren’t designed for those more intricate cases.)

But network_interface_data = azure.network_interface_azure_set.all() seem to work auto-magically - response from ChatGPT - but to convert it to a list - Im doing network_interface_data = list(azure.network_interface_azure_set.all()) which fails.

How, specifically?

To repeat my earlier question,

‘QuerySet’ object has no attribute ‘META’

Ok, then there’s something else involved here. You either have a model manager, some other field or some other overridden function that is interferring with the normal process of this relationship. That is not the expected results of that statement in the common case.

In that I’ll switch to using RAW SQL for this particular query.

How can I debug the root cause from this ?

network_interface_data =  <QuerySet [<Network_interface_azure: Network_interface_azure object (1)>, <Network_interface_azure: Network_interface_azure object (2)>]>
type of network_interface_azure_set =  <class 'django.db.models.query.QuerySet'>
Internal Server Error: /server/azure/715/update
Traceback (most recent call last):
  File "D:\workspace\django\project1\env\lib\site-packages\django\core\handlers\", line 55, in inner
    response = get_response(request)
  File "D:\workspace\django\project1\env\lib\site-packages\django\core\handlers\", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "D:\workspace\django\project1\server\", line 195, in azureUpdate
    print("llist = ", list(azure.network_interface_azure_set.all()))
  File "D:\workspace\django\project1\env\lib\site-packages\ms_identity_web\", line 290, in assert_login
    return f(*args, **kwargs)
  File "D:\workspace\django\project1\server\", line 47, in list
    return render(request, 'inventory/aws_list.html')
  File "D:\workspace\django\project1\env\lib\site-packages\django\", line 24, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "D:\workspace\django\project1\env\lib\site-packages\django\template\", line 62, in render_to_string
    return template.render(context, request)
  File "D:\workspace\django\project1\env\lib\site-packages\django\template\backends\", line 61, in render
    return self.template.render(context)
  File "D:\workspace\django\project1\env\lib\site-packages\django\template\", line 173, in render
    with context.bind_template(self):
  File "C:\Users\AnjaneshLekshminaray\AppData\Local\Programs\Python\Python310\lib\", line 135, in __enter__
    return next(self.gen)
  File "D:\workspace\django\project1\env\lib\site-packages\django\template\", line 254, in bind_template
  File "D:\workspace\django\project1\env\lib\site-packages\django\template\", line 41, in debug
    if settings.DEBUG and request.META.get("REMOTE_ADDR") in settings.INTERNAL_IPS:
AttributeError: 'QuerySet' object has no attribute 'META'
[02/Jun/2023 06:15:49] "GET /server/azure/715/update HTTP/1.1" 500 116079

Well, looking at the traceback, the line actually causing that specific exception is:

The only reference to META here in that statement is the request.META.get(... clause in the if.

My initial reaction to seeing this would make me assume that somewhere in the call stack previous to this, the request obejct is being reassigned to a different value.

I’d start by closely examining the view containing this statement:

Am I sure that this is where the problem lies? No, far from it. But it’s where I would start.

(Note: This is another good example as to why people should always post the complete traceback when trying to diagnose errors.)

Thanks for making me notice that for some reason when I have this line inserted - print(list(azure.network_interface_azure_set.all())) - its trying to render the line return render(request, 'inventory/aws_list.html') which is in

def list(request):

where as print(list(azure.network_interface_azure_set.all())) is inside

def azureUpdate(request, id):
    # Code
    # Code
    return render(request, 'inventory/azure/update.html', {
        "ID": id,
        "azure": azure,
        # Other data

The view function obviously shouldn’t be named list !