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
- [Django]-Default User Model OneToOneField Attribute Error
- [Django]-ModuleNotFoundError: No module named '__main__.models'; '__main__' is not a package
- [Django]-Instance.__dict__ in django templating language
Source:stackexchange.com