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