Django VALUES (*field) uses

Source: Internet
Author: User

This method returns the Valuesqueryset, which is the subclass of Queryset, that is, you can use the method in Queryset. It is important to note that the return is not a list, do not directly when the list to use. For Valuesqueryset traversal, each element is a "dictionary" Dict.

Returns all fields of this model when no arguments are passed in

AppDef.objects.values()
[{' Creator ': U ' admin ', ' apptype_name ': U ' uc3g ', ' apptype_chn_name ': U ' 3g\u95e8\u6237 ', ' note ': U ', ...},...]

When parameters are passed in, only the parameters you specify are listed

AppDef.objects.values (' Apptype_name ')
[{' Apptype_name ': U ' uc3g '},...]

You can also addFilter,FilterIt's the same in front and back.

AppDef.objects.filter (pk=1). VALUES (' Apptype_name ')
AppDef.objects.values (' Apptype_name '). Filter (Pk=1)

If you want to find the associated fields together, onetoonefield, foreignkey  and manytomanyfield  relationships are OK.
manytomanyfield  support    logtypedef defined a app foreignkey

 

logtypedef.objects.filter (pk=6). VALUES (' PK ',  ' app__apptype_name ')
 [{' pk ': 6l,  ' app__apptype_name ':  u ' Wapsearch '}]

If you just want to get App_id can do this, the following three methods are the same, just return the result name corresponding to your query statement

LogTypeDef.objects.filter (pk=6). VALUES (' PK ', ' app_id ')
LogTypeDef.objects.filter (pk=6). VALUES (' PK ', ' app ')
LogTypeDef.objects.filter (pk=6). VALUES (' PK ', ' app__id ')

Note that when the association is many-to-many, it will only help you to list a single article, and will not help you merge into aList.
For example, the result of a type is returned: the samePKdoesn't help you merge .app_id

[{pk:6, app_id:2}, {pk:6, app_id:3}]

2. precautions

When using both distinct () and values (), it is important to note that order_by () (or the default model ordering) is automatically added as a distinct item in select, so the returned results you think are duplicates, actually order The By field is not listed.
If you use values () after extra (), be sure to add the fields extra use, and if extra () after values (), extra fields are automatically added to select.

3. Values_list

just like the values , it is not the dictionary but the tuple that is returned.

Django VALUES (*field) uses

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.