__iexact 精確等于 忽略大小寫 ilike 'aaa'
__contains 包含 like '%aaa%'
__icontains 包含 忽略大小寫 ilike '%aaa%',但是對于sqlite來說,contains的作用效果等同于icontains。
__gt 大于
__gte 大于等于
__lt 小于
__lte 小于等于
__in 存在于一個list范圍內
__startswith 以...開頭
__istartswith 以...開頭 忽略大小寫
__endswith 以...結尾
__iendswith 以...結尾,忽略大小寫
__range 在...范圍內
__year 日期字段的年份
__month 日期字段的月份
__day 日期字段的日
__isnull=True/False
__isnull=True 與 __exact=None的區別
There
is an important difference between __isnull=True and __exact=None.
__exact=None will always return an empty result set, because SQL
requires that no value is equal to NULL. __isnull determines if the
field is currently holding the value of NULL without performing a
comparison.
默認的查找是精確查找
下面的三個查詢結果相同
Blog.objects.get(id__exact=14) # Explicit form
Blog.objects.get(id=14) # __exact is implied
Blog.objects.get(pk=14) # pk implies id__exact
查詢集的比較:
下面的兩個比較語句效果相同:
some_entry == other_entry
some_entry.id == other_entry.id
如果一個對象的主鍵名不叫id,沒關系,some_entry == other_entry的比較會自動查找各自主鍵進行比較。如果對象的主鍵名為name,那么下面的語句效果相同:
some_obj == other_obj
some_obj.name == other_obj.name
Q表達式如果和其他條件語句一同使用,必須放在前面。
Forward access to one-to-many relationships is cached the first time the
related object is accessed. Subsequent accesses to the foreign key on the same
object instance are cached. Example:
e = Entry.objects.get(id=2)
print e.blog # Hits the database to retrieve the associated Blog.
print e.blog # Doesn't hit the database; uses cached version.
Note that the select_related() QuerySet method recursively prepopulates
the cache of all one-to-many relationships ahead of time. Example:
e = Entry.objects.select_related().get(id=2)
print e.blog # Doesn't hit the database; uses cached version.
print e.blog # Doesn't hit the database; uses cached version.
select_related() is documented in the "QuerySet methods that return new
QuerySets" section above.
b = Blog.objects.get(id=1)You cannot access a reverse ForeignKey Manager from the class; it must
b.entry_set.all() # Returns all Entry objects related to Blog.
#entry_set返回的結果集可以執行過濾器功能:
b.entry_set.filter(headline__contains='Lennon')
b.entry_set.count()
對于entry_set的名稱可以通過在定義模型的外鍵時自定義:
blog = ForeignKey(Blog, related_name='entries')
此后就可以在引用外鍵所在隊形時使用如下方式:
b = Blog.objects.get(id=1)
b.entries.all() # Returns all Entry objects related to Blog.
be accessed from an instance. Example:
Blog.entry_set # Raises AttributeError: "Manager must be accessed via instance".
只能是:
b.entry_set
實體引用集的方法:
add:
b = Blog.objects.get(id=1)
e = Entry.objects.get(id=234)
b.entry_set.add(e) # Associates Entry e with Blog b.
create:
b = Blog.objects.get(id=1)
e = b.entry_set.create(headline='Hello', body_text='Hi', pub_date=datetime.date(2005, 1, 1))#不再需要save()方法。
remove:
this method only exists on
ForeignKey objects where null=True.
b = Blog.objects.get(id=1)
e = Entry.objects.get(id=234)
b.entry_set.remove(e) # Disassociates Entry e from Blog b.
clear:
同remove的限制條件
b = Blog.objects.get(id=1)
b.entry_set.clear()
查詢條件中使用實體的幾個示例:
if you have a Blog object b with id=5, the following
three queries would be identical:
Entry.objects.filter(blog=b) # Query using object instance
Entry.objects.filter(blog=b.id) # Query using id from instance
Entry.objects.filter(blog=5) # Query using id directly
delete方法:
是一個查詢結果集的方法,而不是實體模型的方法,如:
Entry.objects.filter(pub_date__year=2005).delete() #正確
Entry.objects.all().delete() #不正確
get_FOO_display():
For every field that has choices set, the object will have a
get_FOO_display() method, where FOO is the name of the field. This
method returns the "human-readable" value of the field. For example, in the
following model:
GENDER_CHOICES = (
('M', 'Male'),
('F', 'Female'),
)
class Person(models.Model):
name = models.CharField(maxlength=20)
gender = models.CharField(maxlength=1, choices=GENDER_CHOICES)
...each Person instance will have a get_gender_display() method. Example:
>>> p = Person(name='John', gender='M')
/>>> p.save()
/>>> p.gender
'M'
/>>> p.get_gender_display()
'Male'
取得最大最小id記錄
MIN == Entry.objects.order_by()[0]
MAX == Entry.objects.order_by('-id'][0]