Django의 그룹 조회 및 집합 조회 및 Q 조회

프로그램 코드
import os

if __name__ == '__main__':
    #  Django       
    os.environ.setdefault("DJANGO_SETTINGS_MODULE","testinclude.settings")
    #  Django,   Django  
    import django
    django.setup()

    from ormtest import models
    from django.db.models import Avg,Sum,Max,Min,Count,Q

    #    
    #     
    # ret = models.Book.objects.all().aggregate(Avg("price"))
    # print(ret)

    #      
    # ret = models.Book.objects.all().count()
    # print(ret)

    #        
    # ret = models.Book.objects.all().aggregate(Max("price"))
    # print(ret)

    #         
    # ret = models.Book.objects.all().aggregate(Min("price"))
    # print(ret)

    #    
    #           
    ret = models.Book.objects.all().annotate(author_num=Count("author"))
    for book in ret:
        print("  :{},    :{}".format(book.title,book.author_num))

        #Q  
    #        100,  200  
    ret = models.Book.objects.filter(price__gt=100,price__lt=200)
    print(ret)

    #        150    50  
    ret = models.Book.objects.filter(Q(price__gt=100)| Q(price__lt=35))
    print(ret)

결실
  :      ,    :1
  :python      ,    :1
  :Linux  ,    :1
  :     ,    :2
  :     ,    :1
  :Django      ,    :1
  :zabbix      ,    :0

좋은 웹페이지 즐겨찾기