__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]