그룹 조회

1811 단어
    #      ,      
    # pk        
    # orm      :
    # group by  ,      
    # filter  ,  where value  ,  group by
    # filter  ,  having,value  ,     
    from django.db.models import Count


    models.Category.objects.all().filter(blog=user.blog).values('title').\
        annotate(cateory_count=Count("pk")).\
        filter(cateory_count__gt=1).\
        values('title','cateory_count')
    ca_num=models.Category.objects.annotate(cateory_count=Count("article")).values('title','cateory_count')
    ca_num2=models.Category.objects.all().values('title').annotate(cateory_count=Count("article__title")).values('title','cateory_count')
    print(ca_num)
    print(ca_num2)

    #           ,      
    # ca_num=models.Category.objects.all().filter(blog=blog).annotate(cateory_count=Count("article")).values('title','cateory_count')
    ca_num = models.Category.objects.all().filter(blog=blog).annotate(cateory_count=Count("article")).values_list(
        'title', 'cateory_count', 'nid')
    print(ca_num)
    #             ,      
    tag_num = models.Tag.objects.all().filter(blog=blog).values('nid'). \
        annotate(tag_count=Count('article__title')). \
        values_list('title', 'tag_count', 'pk')
    print(tag_num)
    #             
    #     
    from django.db.models.functions import TruncMonth
    month_num = models.Article.objects.all().order_by('-create_time').filter(user=user).annotate(
        month=TruncMonth('create_time')).values('month').annotate(c=Count('month')).values_list('month', 'c')

    print(month_num)

좋은 웹페이지 즐겨찾기