Django QuerySet API
来源:互联网 发布:太阁立志传 mac 编辑:程序博客网 时间:2024/05/22 13:46
https://docs.djangoproject.com/en/1.4/ref/models/querysets/#distinct
1.distinct
- distinct([*fields])
Returns a new QuerySet that uses SELECT DISTINCT in its SQL query. This eliminates duplicate rows from the query results.
As of Django 1.4, you can pass positional arguments (*fields) in order to specify the names of fields to which the DISTINCTshould apply. This translates to a SELECT DISTINCT ON SQL query.
Here’s the difference. For a normal distinct() call, the database compares each field in each row when determining which rows are distinct. For a distinct() call with specified field names, the database will only compare the specified field names.
2.using
- using(alias)
This method is for controlling which database the QuerySet will be evaluated against if you are using more than one database. The only argument this method takes is the alias of a database, as defined in DATABASES.
3.values
- values(*fields)¶
Returns a ValuesQuerySet — a QuerySet subclass that returns dictionaries when used as an iterable, rather than model-instance objects.
Each of those dictionaries represents an object, with the keys corresponding to the attribute names of model objects.
This example compares the dictionaries of values() with the normal model objects:
The values() method takes optional positional arguments, *fields, which specify field names to which the SELECT should be limited. If you specify the fields, each dictionary will contain only the field keys/values for the fields you specify. If you don’t specify the fields, each dictionary will contain a key and value for every field in the database table.>>> Blog.objects.values()[{'id': 1, 'name': 'Beatles Blog', 'tagline': 'All the latest Beatles news.'}],>>> Blog.objects.values('id', 'name')[{'id': 1, 'name': 'Beatles Blog'}]
4.values_list
values_list(*fields)
This is similar to values() except that instead of returning dictionaries, it returns tuples when iterated over. Each tuple contains the value from the respective field passed into the values_list() call -- so the first item is the first field, etc. For example:
>>> Entry.objects.values_list('id', 'headline')[(1, u'First entry'), ...]
If you only pass in a single field, you can also pass in the flat parameter. If True, this will mean the returned results are single values, rather than one-tuples. An example should make the difference clearer:
>>> Entry.objects.values_list('id').order_by('id')[(1,), (2,), (3,), ...]>>> Entry.objects.values_list('id', flat=True).order_by('id')[1, 2, 3, ...]
It is an error to pass in flat when there is more than one field.
If you don't pass any values to values_list(), it will return all the fields in the model, in the order they were declared.
- Django QuerySet API
- Django QuerySet API
- Django - 模型(QuerySet API)
- Django QuerySet API
- Django QuerySet API
- Django QuerySet API 帮助文档
- Django QuerySet的基本API
- Django学习(九)(QuerySet API)
- Django | 查询API参考 (QuerySet API reference) | Django文档
- django-QuerySet
- Django QuerySet
- QuerySet API
- [py]Django 提供的 QuerySet API操作db
- Django数据库queryset操作
- django中的queryset操作
- Django 数据库QuerySet操作
- django queryset values&values_list
- Django数据库queryset操作
- SqlBulkCopy数据批量处理
- 4种必须知道的Android屏幕自适应解决方案
- Android判断网络连接相关方法
- mysql的blob读取和sqlserver的image读取方式 blob和image兼容问题及varbinary(max)
- postgres复制数据库
- Django QuerySet API
- Uva 11300 - Spreading the Wealth(中位数距离)
- highcharts绘制统计图的应用
- Ubuntu PPTP VPN Server 设置
- 使用WinPcap编程——把网络数据包存储到一个文件中
- gcc LDR指令和伪指令
- “LogCat 输出乱码”问题解决
- Flip Ocean & MAELSTROM动态(vimeo)
- 怎样实现Word2003转PDF?无需插件即可另存为PDF的虚拟打印机法