共 11 篇文章 |
|
django 分组查询 djang 指定字段分组查询方法context = {}organ_experts = OrganExpert.objects.filter(status=1)organ_experts.query.group_by = [''expert_id'']context[''experts''] = organ_experts. 阅920 转0 评0 公众公开 14-04-30 19:30 |
from collections import defaultdictfrom django.db import connectionfrom django.db.models import Countselect = {''hour'': connection.annotate(number=Count(''id''))# [{''number'': 10, ''hour'': datetime.datetime(2013, 9, 28, 19, 0, 0},# {''number... 阅5640 转24 评0 公众公开 14-04-30 17:38 |
django QuerySet里那些常用又不常见的技巧。https://docs.djangoproject.com/en/1.6/ref/models/querysets/#field-lookupsQuerySet !from django.db.models import F例如model 有两列 一列叫做user,一列叫做assigned_user,需求是取出user=assigned_user的记录direct_comment = _tasks.filter(user=F(''assigned_user''))django... 阅3757 转4 评0 公众公开 14-04-25 09:49 |
Django Q查询。Poll.objects.get( Q(question__startswith=''Who''), Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6)))正确:Poll.objects.get( Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6)), question__startswith=''Who'')错误:Poll.objects.get( question__startswith=... 阅5031 转8 评0 公众公开 14-04-25 09:48 |
select table1.* from table1where table1.id not in ( select table2.key_to_table1 from table2 where table2.id = some_parm )What would the django syntax look like assuming models called table1 and table2?table1.objects.exclude(id__in = table2.objects.filter(your_condition).values_list(''id'', flat=True)) 阅2035 转2 评0 公众公开 14-04-24 20:28 |
Django QuerySets : 真他妈棒?Book.objects.order_by(''author'', ''-year_published'') 关于 QuerySet s最酷的是,由于这些函数操作、返回的都是一个QuerySet,你可以把他们链起来:ancestors = self.get_ancestors()filter(pk=self.parent.pk) | self.parent.get_ancestors()联合 - QuerySet的联合运算符是... 阅116 转1 评0 公众公开 14-04-24 19:57 |
Django 查询时间段。now = datetime.datetime.now()start = now – datetime.timedelta(hours=23, minutes=59, seconds=59)a=yourobject.objects .filter(youdatetimcolumn__gt=start)[, , ]start_date = datetime.date(2005, 1, 1)end_date = datetime.date(2005, 3, 31)Entry.objects.filter(pub_date__range=(start_date, end_date))Entry.o... 阅3325 转4 评0 公众公开 14-04-24 16:19 |
>>> from jobs.models import Job>>> for job in Job.objects.all():... print job.>>> from jobs.models import Job>>> from datetime import datetime>>> q1 = Job.objects.filter(pub_date__gte=datetime(2006, 1, 1))>>> q2 = Job.objects.exclude(pub_date__lt=datetime(2006, 1, ... 阅439 转1 评0 公众公开 14-04-24 14:43 |
django model filter 条件过滤,及多表连接查询、反向查询,某字段的distinct1.多表连接查询:当我知道这点的时候顿时觉得django太NX了。>> q1 = Entry.objects.filter(headline__startswith="What")>> q2 = q1.exclude(pub_date__gte=datetime.date.today())>> q3 = q1.filter(pub_date__gte=datetime.date.today... 阅254 转1 评0 公众公开 14-04-24 13:17 |
Python django学习之:model (与数据库操作)Creating table books_publisherCreating table books_authorCreating table books_book_authorsCreating table books_bookInstalling custom SQL ...>>> from books.models import Publisher>>> p1 = Publisher(name=''Apress'', address=''2855 Teleg... 阅15612 转30 评0 公众公开 14-04-21 12:12 |