单表的基本操作 http://beanxyz.blog.51cto.com/5570417/1945887

常见字段的使用 http://beanxyz.blog.51cto.com/5570417/1945909

最基本的查询方式 http://beanxyz.blog.51cto.com/5570417/1950806

一对多的基本操作和实例  http://beanxyz.blog.51cto.com/5570417/1946602

多对多的基本操作和实例 http://beanxyz.blog.51cto.com/5570417/1952243



条件的过滤

下面是常见的条件设置,除了可以基本的filter之外,我们还有大量的条件语句可以使用。


查询数据库获取的QuerySet类型,对于这个类型我们类似Jquery一样使用链式编程,可以无限制的通过.来添加新的条件来过滤,可以看见大部分条件都是通过双下划线__来实现的


# 获取个数
# models.Tb1.objects.filter(name='seven').count()
    <span class="token comment"># 大于,小于</span>
    <span class="token comment"># models.Tb1.objects.filter(id__gt=1)              # 获取id大于1的值</span>
    <span class="token comment"># models.Tb1.objects.filter(id__gte=1)              # 获取id大于等于1的值</span>
    <span class="token comment"># models.Tb1.objects.filter(id__lt=10)             # 获取id小于10的值</span>
    <span class="token comment"># models.Tb1.objects.filter(id__lte=10)             # 获取id小于10的值</span>
    <span class="token comment"># models.Tb1.objects.filter(id__lt=10, id__gt=1)   # 获取id大于1 且 小于10的值</span>

    <span class="token comment"># in</span>
    <span class="token comment"># models.Tb1.objects.filter(id__in=[11, 22, 33])   # 获取id等于11、22、33的数据</span>
    <span class="token comment"># models.Tb1.objects.exclude(id__in=[11, 22, 33])  # not in</span>

    <span class="token comment"># isnull</span>
    <span class="token comment"># Entry.objects.filter(pub_date__isnull=True)</span>

    <span class="token comment"># contains</span>
    <span class="token comment"># models.Tb1.objects.filter(name__contains="ven")</span>
    <span class="token comment"># models.Tb1.objects.filter(name__icontains="ven") # icontains大小写不敏感</span>
    <span class="token comment"># models.Tb1.objects.exclude(name__icontains="ven")</span>

    <span class="token comment"># range</span>
    <span class="token comment"># models.Tb1.objects.filter(id__range=[1, 2])   # 范围bettwen and</span>

    <span class="token comment"># 其他类似</span>
    <span class="token comment"># startswith,istartswith, endswith, iendswith,</span>

    <span class="token comment"># order by</span>
    <span class="token comment"># models.Tb1.objects.filter(name='seven').order_by('id')    # asc</span>
    <span class="token comment"># models.Tb1.objects.filter(name='seven').order_by('-id')   # desc</span>

    <span class="token comment"># group by</span>
    <span class="token comment"># from django.db.models import Count, Min, Max, Sum</span>
    <span class="token comment"># models.Tb1.objects.filter(c1=1).values('id').annotate(c=Count('num'))</span>
    <span class="token comment"># SELECT "app01_tb1"."id", COUNT("app01_tb1"."num") AS "c" FROM "app01_tb1" WHERE "app01_tb1"."c1" = 1 GROUP BY "app01_tb1"."id"</span>

    <span class="token comment"># limit 、offset</span>
    <span class="token comment"># models.Tb1.objects.all()[10:20]</span>

    <span class="token comment"># regex正则匹配,iregex 不区分大小写</span>
    <span class="token comment"># Entry.objects.get(title__regex=r'^(An?|The) +')</span>
    <span class="token comment"># Entry.objects.get(title__iregex=r'^(an?|the) +')</span>

    <span class="token comment"># date</span>
    <span class="token comment"># Entry.objects.filter(pub_date__date=datetime.date(2005, 1, 1))</span>
    <span class="token comment"># Entry.objects.filter(pub_date__date__gt=datetime.date(2005, 1, 1))</span>

    <span class="token comment"># year</span>
    <span class="token comment"># Entry.objects.filter(pub_date__year=2005)</span>
    <span class="token comment"># Entry.objects.filter(pub_date__year__gte=2005)</span>

    <span class="token comment"># month</span>
    <span class="token comment"># Entry.objects.filter(pub_date__month=12)</span>
    <span class="token comment"># Entry.objects.filter(pub_date__month__gte=6)</span>

    <span class="token comment"># day</span>
    <span class="token comment"># Entry.objects.filter(pub_date__day=3)</span>
    <span class="token comment"># Entry.objects.filter(pub_date__day__gte=3)</span>

    <span class="token comment"># week_day</span>
    <span class="token comment"># Entry.objects.filter(pub_date__week_day=2)</span>
    <span class="token comment"># Entry.objects.filter(pub_date__week_day__gte=2)</span>

    <span class="token comment"># hour</span>
    <span class="token comment"># Event.objects.filter(timestamp__hour=23)</span>
    <span class="token comment"># Event.objects.filter(time__hour=5)</span>
    <span class="token comment"># Event.objects.filter(timestamp__hour__gte=12)</span>

    <span class="token comment"># minute</span>
    <span class="token comment"># Event.objects.filter(timestamp__minute=29)</span>
    <span class="token comment"># Event.objects.filter(time__minute=46)</span>
    <span class="token comment"># Event.objects.filter(timestamp__minute__gte=29)</span>

    <span class="token comment"># second</span>
    <span class="token comment"># Event.objects.filter(timestamp__second=31)</span>
    <span class="token comment"># Event.objects.filter(time__second=2)</span>
    <span class="token comment"># Event.objects.filter(timestamp__second__gte=31)</span></code></pre><div class="toolbar"></div></div>