django查询的时候的一些tips
exact
Performs an exact match:
>>> Entry.objects.get(headline__exact="Man bites dog")
This matches any object with the exact headline Man bites dog.
If you dont provide a lookup type that is, if your keyword argument doesnt contain a double underscore the
lookup type is assumed to be exact .
For example, the following two statements are equivalent:
>>> Blog.objects.get(id__exact=14) # Explicit form
>>> Blog.objects.get(id=14) # __exact is implied
This is for convenience, because exact lookups are the common case.
iexact
字符串比较(大小写无关)
>>> Blog.objects.get(name__iexact='beatles blog')
This will match 'Beatles Blog' , 'beatles blog' , 'BeAtLes BLoG' , and so forth.
contains
Performs a case-sensitive containment test:
Entry.objects.get(headline__contains='Lennon')
This will match the headline 'Today Lennon honored' but not 'today lennon honored' .
SQLite doesnt support case-sensitive LIKE statements; when using SQLite,``contains`` acts like icontains .
Escaping Percent Signs and Underscores in LIKE Statements
The field lookups that equate to LIKE SQL statements (iexact , contains , icontains , startswith ,
istartswith , endswith , and iendswith ) will automatically escape the two special characters used in LIKE
statements the percent sign and the underscore. (In a LIKE statement, the percent sign signifies a multiplecharacter
wildcard and the underscore signifies a single-character wildcard.)
This means things should work intuitively, so the abstraction doesnt leak. For example, to retrieve all the
entries that contain a percent sign, just use the percent sign as any other character:
Entry.objects.filter(headline__contains='%')
Django takes care of the quoting for you. The resulting SQL will look something like this:
SELECT ... WHERE headline LIKE '%\%%';
The same goes for underscores. Both percentage signs and underscores are handled for you transparently.
icontains
Performs a case-insensitive containment test:
>>> Entry.objects.get(headline__icontains='Lennon')
Unlike contains , icontains will match 'today lennon honored' .
gt gte lt lte
gt大于
gte大于等于
lt小于
lte小于等于