1、查询所有记录:模型名.objects.all()
>>> from django.contrib.auth.models import User
>>> users = User.objects.all()
>>> users
<QuerySet [<User: admin>, <User: zhanglaoshi>, <User: wanglaoshi>, <User: zhaolaoshi>,
<User: zhoulaoshi>, <User: wulaoshi>, <User: danglaoshi>, <User: canglaoshi>, <User:
hulaoshi>, <User: yinlaoshi>, <User: lulaoshi>, <User: chenlaoshi>, <User: kuanglaoshi>,
<User: xiaodou>, <User: xiaojin>]>
查询结果保留在users序列中,获得序列中的一个元素,使用下述方式:
>>> lu = users[10]
>>> lu
<User: lulaoshi>
>>> lu.email
'lu@laoshi.com'
2、精确查询:模型名.objects.get(字段名=字段值)
括号里面的是查询条件,可以设置一个以上的查询条件,中间用','分开。
查询不到结果时:报错。
>>> BlogArticles.objects.get(id=1)
<BlogArticles: php is the best computer language>
>>> BlogArticles.objects.get(title="Canglaoshi is a good teacher")
<BlogArticles: Canglaoshi is a good teacher>
>>> BlogArticles.objects.get(title="teacher")
Traceback (most recent call last):
File "/usr/lib/python3.5/code.py", line 91, in runcode
exec(code, self.locals)
File "<console>", line 1, in <module>
File "/usr/local/lib/python3.5/dist-packages/django/db/models/manager.py", line 85,
in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py", line 385, in
get
self.model._meta.object_name
blog.models.DoesNotExist: BlogArticles matching query does not exist.
3、根据条件查询:模型名.objects.filter(过滤条件)
过滤不到结果时:返回空。
>>> users_laoshi = User.objects.filter(username__endswith="laoshi")
>>> users_laoshi
<QuerySet [<User: zhanglaoshi>, <User: wanglaoshi>, <User: zhaolaoshi>, <User:
zhoulaoshi>, <User: wulaoshi>, <User: danglaoshi>, <User: canglaoshi>, <User: hulaoshi>,
<User: yinlaoshi>, <User: lulaoshi>, <User: chenlaoshi>, <User: kuanglaoshi>]>
这里查询出username中以"laoshi"这个字符串结尾的对象。
除filter()能够实现根据条件查询外,还有exclude()
>>> users = User.objects.filter(username__endswith="laoshi").exclude(id__gt=7)
>>> users
<QuerySet [<User: zhanglaoshi>, <User: wanglaoshi>, <User: zhaolaoshi>, <User:
zhoulaoshi>, <User: wulaoshi>, <User: danglaoshi>]>
4、查询结果排序
对于查询返回的QuerySet结果,默认是按照数据模型类中定义的字段排序的,如果要重新排序,可以使用order_by()方法。
>>> u = User.objects.filter(username__startswith="z").order_by("username")
>>> u
<QuerySet [<User: zhanglaoshi>, <User: zhaolaoshi>, <User: zhoulaoshi>]>
>>> u = User.objects.filter(username__startswith="z").order_by("-username")
>>> u
<QuerySet [<User: zhoulaoshi>, <User: zhaolaoshi>, <User: zhanglaoshi>]>
order_by()的参数中可以写多个字段名称,即按照字段前后顺序分别作为排序的"主关键词"和"次关键词"。
5、使用外键查询
定义ArticleColumn模型时,定义外键user如下:
user = models.ForeignKey(User, on_delete=models.CASCADE,
related_name='article_column')
说明User一条记录,可以对应多个ArticleColumn记录,是一对多的关系,并且通过user可以查到其对应的ArticleColumn记录。related_name='article_columns'
提供了从 User
实例访问关联的 ArticleColumn
实例的方便方式。
user = User.objects.get(username='john_doe')
columns = user.article_columns.all() # 获取所有与这个用户关联的 ArticleColumn 实例
如果没有定义related_name,则使用下述代码查询相关的
ArticleColumn记录。
user = User.objects.get(username='john_doe')
columns = user.articlecolumn_set.all() # 使用默认的反向关系名称
articlecolumn_set中的articlecolumn是模型名。