Django Object filter query [go]

Source: Internet
Author: User

In Python, DJANGO does the station, in the usual case, need to use the OrM query method, such as object.filter (tag__contains= ' keywords ') ....

In this case, if you trace the SQL statement, you will find that the SQL statement will generate a select .... like Bianry '%keywords% ', if this is the case, it will be problematic in some cases, which means that the data you are querying may be less than you expect.

If you use raw SQL to check the total number of select count (*) from table where the like '%keywords% ' gets a lot more data than you can get with ORM using the above method, the problem lies in the resulting condition problem.

The Django instructions were later queried, if the ORM statement became: object.filter (tag__icontains= ' keywords ') .... Just fine.

Note the difference between contains and icontains. Later from the Django official website to find out the description:

Program code
Operators = {
' exact ': ' =%s ',
' Iexact ': ' Like%s ',
' contains ': ' Like BINARY%s ',
' Icontains ': ' Like%s ',
' Regex ': ' REGEXP BINARY%s ',
' Iregex ': ' REGEXP%s ',
' GT ': ' >%s ',
' GTE ': ' >=%s ',
' LT ': ' <%s ',
' LTE ': ' <=%s ',
' StartsWith ': ' Like BINARY%s ',
' EndsWith ': ' Like BINARY%s ',
' Istartswith ': ' Like%s ',
' Iendswith ': ' Like%s ',
}



Refer to the instructions, you can write your own statement.

Django Object filter query [go]

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.