[Django]-Reverse Relationship in django 'QuerySet' object has no attribute 'name'

2👍

When do you use get() Django return an object and you can get the variables of that object, for example obj1.name, but when you use filter, Django return a Queryset, you have to iterate the queryset with a for:

mini_producers = Producer.objects.filter(car__name='Mini')

for producer in mini_producers:
    print(producer.name)

1👍

Queryset is a list of objects, not a single object.

So you can do:

obj1 = Producer.objects.filter(car__name='Mini').first(). # <- get first
In [6]: obj1.name  

or in case you have to handle multiple.

for obj in obj1:
    print(obj.name)
    # do your logic

Leave a comment