"Service Backend" Django Multi-table data query

Source: Internet
Author: User

One, multi-table query

With data table class and Classusertemp table

The requirement is to filter out all the data classusertemp the class under which a user luser management.

The owner field of the class table represents the associated administrator user.

You only need to use the associated property, the condition path is the class---class administrator--compared with the user

puserclasses = ClassUserTemp.objects.filter (Idclass__owner=luser)

Second, foreign key query

Need to change, to query a user in the Classusertemp records, the corresponding class

Comb, the condition path, classusertemp the user's record Iduser=luser can.

But the return class of the objects, at this time with Classusertemp foreign key Fkclassusertemp2class Connect class and Classusertemp table

Class.objects.filter (Fkclassusertemp2class__iduser=luser)

Appendix: Values Filtering

A table returns the results of a query and may just filter some of the fields to the user, so that you can use the values method on the query result set

User Table Structure Section

Define User table visible fields

The query result set is handled as follows, and it is worth noting that the "|" Represents merge Collection

Luser = User.objects.filter (Phonenumber=pkey) | User.objects.filter (Username__contains=pkey)
Luser = Luser.distinct (). VALUES (*globalvar.tablefields.userfields)

Queryset Other field meanings

Filter indicates that =,exclude represents! =.
Queryset.distinct () to repeat
__exact exactly equals like ' AAA '
__iexact precision equals ignoring case ilike ' AAA '
__contains contains like '%aaa% '
__icontains includes ignoring case ilike '%aaa% ', but for SQLite, the effect of contains is equivalent to Icontains.
__GT Greater than
__gte greater than or equal to
__lt less than
__lte less than or equal to
__in exists in a list range
__startswith to ... Beginning
__istartswith to ... Start ignoring case
__endswith to ... End
__iendswith to ... End, ignoring case
__range in ... Within range
__year Year of Date field
__month Month of Date field
Day of the __day date field
__isnull=true/false

"Service Backend" Django Multi-table data query

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.