django QuerySet里那些常用又不常见的技巧
2015-07-17 10:29
633 查看
QuerySet
像Entry.Objects.all(),这些操作返回的是一个QuerySet对象,这个对象比较特别,并不是执行Objects.all(),或者filter之后就会与数据库交互,
具体参看官方文档,与数据库交互的情况:
https://docs.djangoproject.com/en/dev/ref/models/querysets/
Internally, a QuerySet can be constructed, filtered, sliced, and generally passed around without actually hitting the database. No database activity actually occurs until you do something to evaluate the queryset. 可以print queryset对象的query属性查看具体sql 1. list(Entry.Objects.all()) 2. for e in Entry.Objects.all():pass # 便利第一个元素的时候会触发数据库操作 3. Entry.Objects.all()[2:10] # 这种情况不会返回所有元素,sql中会加上limit的,分页可以利用这一点
Q和F
F class from django.db.models import F Instances of F() act as a reference to a model field within a query. These references can then be used in query filters to compare the values of two different fields on the same model instance.
这就是说F是专门取对象中某列值的,例子: 'QuerySet 判断一个model两个字段是否相等'
Q class from django.db.models import Q Keyword argument queries – in filter(), etc. – are “AND”ed together. If you need to execute more complex queries (for example, queries with OR statements), you can use Q objects.
从文档把Q放在Complex lookups with Q objects,下就可以看出,Q是做复杂查询的
and --> XX.objects.filter(Q(f=1),Q(f=2)) # 肯定木有结果 f == 1 and f == 2 or --> XX.objects.filter(Q(f=1) | Q(f=2)) # f ==1 | f == 2 not --> XX.objects.filter(~Q(f=1),Q(f=2)) # f != 1 and f == 2
判断某字段是否为null
_tasks = tasks.exclude(group__isnull=True)
各种双下滑线对应的各种方法,参看文档 querysets field lookup
https://docs.djangoproject.com/en/1.6/ref/models/querysets/#field-lookups
QuerySet !=
例如model 有两列 一列叫做user,一列叫做assigned_user,需求是取出user!=1的记录,django里面不能使用!=,需要用Q
from django.db.models import Q direct_comment = _tasks.filter(~Q(user=1))
Q还可以这样,user = 1或者2的元素
direct_comment = _tasks.filter(Q(user=1) | Q(user=2))
QuerySet 判断一个model两个字段是否相等
from django.db.models import F 例如model 有两列 一列叫做user,一列叫做assigned_user, 需求是取出user=assigned_user的记录 direct_comment = _tasks.filter(user=F('assigned_user'))
django group_by
对某些取到的QuerySet分组还是很常见的def group_by(query_set, group_by): '''util:django 获取分类列表''' assert isinstance(query_set, QuerySet) django_groups = query_set.values(group_by).annotate(Count(group_by)) groups = [] for dict_ in django_groups: groups.append(dict_.get(group_by)) return groups 例如: assign_to = _tasks.exclude(user=F('assigned_user')) groups = group_by(assign_to, 'group') 取出的是一个列表groups = [1L, 3L, 4L]
相关文章推荐
- UI--Android中的状态切换按钮自定义
- 百度编辑器ueditor1.4.2-更改图片上传路径和图片读取路径
- 用 query 方法 获得xml 节点的值
- 动态计算UITableViewCell高度详解
- HDU 1005 Number Sequence
- UITableviewCell使用以及自定义高度
- [LeetCode][Java] Unique Paths II
- UEFI&EDKII Introduction:UEFI&EDKII概述[1]
- 用代码生成UINavigationController 与UITabBarController相结合的简单QQ框架(部分)
- ios7 上 UIActivity 用的image
- ios之把UIColor类型得到一张纯色的UIImage类型的图片
- window.event.returnvalue=false;不起作用
- 关于UITableView的黑线条
- UI设计中我们为什么需要设计动效
- [NGUI插件] 最全的NGUI各个版本插件下载地址(最新版本3.8.2)
- 浅谈NGUI的NGUITools类
- UIBarButtonSystemItem的样式图片
- EF6 CodeFirst+Repository+Ninject+MVC4+EasyUI实践(二)
- guice框架的入门使用
- SSH框架 sequence diagram