what's the difference between using:
Blabla.objects.values('field1', 'field2', 'field3')
and
Blabla.objects.only('field1', 'field2', 'field3')
"values()" returns a QuerySet of dictionaries. "values_list()" returns a QuerySet of tuples. "values_list()" with "flat=True" returns a QuerySet of values.
A value is a more abstract concept than an object. Like those mentioned in comments, the difference/relationship between a value and an object is similar to that between water and a cup. You need a container, which in this case is a cup, to hold the water, or it will leak.
flat parameterWhen grabbing a single values from the db, you can pass`flat=true` which will just give you back single values, instead of tuples: >>> queryset = Book. objects.
Assuming Blabla
has the fields in your question, as well as field4
,
Blabla.objects.only('field1', 'field2', 'field3')[0].field4
will return the value of that object's field4
(with a new database query to retrieve that info), whereas
Blabla.objects.values('field1', 'field2', 'field3')[0].field4
will give
AttributeError: 'dict' object has no attribute 'field4'
This is because .values()
returns a QuerySet
that returns dictionaries, which is essentially a list of dicts, rather than model instances (Blabla
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With