Values (*fields)
Returns a valuesqueryset --a subclass of QuerySet that returns a dictionary instead of a model instance object at iteration time.
Each dictionary represents an object, and the key corresponds to the property name of the model object.
The following example compares the values () with a normal model object:
#This list contains a Blog object.>>> Blog.objects.filter (name__startswith='Beatles')[<blog:beatles blog>]#This list contains a dictionary.>>> Blog.objects.filter (name__startswith='Beatles'). VALUES () [{'ID': 1,'name':'Beatles Blog','tagline':'All the latest Beatles news.'}]
values () receives the optional positional parameter *fields, which specifies which fields should be restricted by SELECT . If you specify a field, each dictionary will contain only the key/value of the specified field. If no fields are specified, each dictionary will contain the keys and values of all the fields in the database table.
For example:
>>>Blog.objects.values () [{'ID': 1,'name':'Beatles Blog','tagline':'All the latest Beatles news.'}],>>> Blog.objects.values ('ID','name')[{'ID': 1,'name':'Beatles Blog'}]
Notable points:
If you have a field foo is a foreignkey, the default values () call returned by the dictionary will have a key called foo_id , Because this is the name of the hidden model property that holds the actual value (thefoo attribute references the associated model). When you call values () and pass the name of the field, pass foo or foo_id , and the result is the same (the Dictionary key matches the field name you passed).
For example:
>>>Entry.objects.values () [{'blog_id': 1,'Headline':'First Entry', ...}, ...]>>> Entry.objects.values ('Blog')[{'Blog': 1}, ...]>>> Entry.objects.values ('blog_id')[{'blog_id': 1}, ...]
-
When values () are used with distinct () , note that sorting can affect the final result. For more information, see Remarks in distinct () .
-
if values () The clause is located in extra () After the call, extra () in select Parameters defined fields must be explicitly included in the values () in call. values () Call back extra () Call ignores extra fields selected.
It is not reasonable to call only () and defer ( ) after the values () , so a notimplementederrorwill be thrown.
Finally, note that Valuesqueryset is a subclass of QuerySet , which implements most of the same methods. You can call filter (),order_by () , and so on. This means that the following two calls are identical:
Blog.objects.values (). order_by ('ID') Blog.objects.order_by ('ID ') '). VALUES ()
Django's authors like to put the method that affects SQL in front, and then place the method that affects the output (for example, values ()), but it doesn't really matter. It's a good chance to show off your personality.
You can use the onetoonefield,ForeignKey , and Manytomanyfield properties to reverse reference the fields of the associated model:
Blog.objects.values ('name','Entry__headline')[{'name':'My Blog','Entry__headline':'An entry'}, {'name':'My Blog','Entry__headline':'Another entry'}, ...]
Django objects.values