Django 데이터베이스 조회 사례

9965 단어 토대
      

from datetime import date

from django.db.models import F, Q, Sum, Max
from django.http import HttpResponse
from django.shortcuts import render

#     
from .models import BookInfo,HeroInfo
# Create your views here.
def index(request):

#    
    #         ,     1980-01-01   
    #          
    #  1     
    # book=BookInfo()
    # book.btitle='   '
    # book.bpub_date= '1980-01-01'
    # book.save()
    # BookInfo.objects.create(
    # btitle='   ',
    # bpub_date='1980-01-10'
    # )

    #      object   
    #              
    # HeroInfo.objects.create(
    # hname='   ',
    # hbook_id=5,
    # hcomment='   '

    # )
    #          
    #5           
    #          
    # book=BookInfo.objects.get(id=5)  #  get              
    # book.btitle='   '
    # book.save()
    #                         
    # HeroInfo.objects.filter(hname='   ').update(hname='   ')

    #         
    # hero=HeroInfo.objects.get(id=19)
    # hero.delete()
    #          
    # HeroInfo.objects.filter(id=17).delete()



    #     
    # 1     1   
    # book=BookInfo.objects.get(id=1)
    # print(book)
    #
    #      
    # book=BookInfo.objects.all()  #     
    # print(book)

    #         
    # count=BookInfo.objects.count()
    # print(count)



    #        sql  like       %       
    #          ,     
    #            (  ,  )
    # book = BookInfo.objects.filter(btitle__startswith='  ')
    # book = BookInfo.objects.filter(btitle__endswith='  ')
    # print(book)

    #         1,3
    # book=BookInfo.objects.filter(id__in=[1,3,5])
    # print(book)

    #       3   
    # book=BookInfo.objects.filter(id__gt=3)
    # print(book)
    #         3   
    # book=BookInfo.objects.filter(id__gte=3)
    # print(book)


    #       3    exclude :  

    # book=BookInfo.objects.exclude(id=3)
    # print(book)

    #   1980     
    # book=BookInfo.objects.filter(bpub_date__year='1980')
    # print(book)


    #   1980         data:            
    # book = BookInfo.objects.filter(bpub_date__gt=date(1980,1,1))
    # print(book)

    #                 F:       ,   
    # book=BookInfo.objects.filter(bread__gte=F('bcomment'))
    # print(book)


    #               
    # book=BookInfo.objects.filter(bread__gte=F('bcomment')*2)
    # print(book)


    #   id=1            1      
    # book=BookInfo.objects.get(id=1)
    # book.bread+=1
    # book.save()
    # book=BookInfo.objects.filter(id=1).update(bread=F('bread')+1)
    # print(book)
    #

    #        20      3   
    # book=BookInfo.objects.filter(bread__gt=20,id__lt=3)
    # print(book)


    #        20      3   ,   Q    
    # book = BookInfo.objects.filter(Q (bread__gt=20)|Q(id__lt=3))
    # print(book)


    #       3   
    # book=BookInfo.objects.exclude(id=3)
    # book=BookInfo.objects.filter(~Q(id=3))
    # print(book)


    #         aggregate
    #           
    # book=BookInfo.objects.aggregate(Sum('bread'))
    # print(book)

    #        
    # book=BookInfo.objects.aggregate(Max('bread'))
    # print(book)

    #      .       ,  (  )
    # book=BookInfo.objects.all().order_by('bread')      
    # book=BookInfo.objects.all().order_by('-bread')      
    # print(book)


    #  
    #       1      
    # book=BookInfo.objects.get(id=1)
    #             
    #          .        _set
    # hero=book.heroinfo_set.all()  #        
    #       related_name=hero ,  hero   heroinfo_set
    # print(hero)

    #                  
    # book=BookInfo.objects.get(btitle='    ')
    # hero=book.hero.all()
    # print(hero)
    #

    #       1      
    # hero=HeroInfo.objects.get(id=1)
    #              ,            
    # book=hero.hbook
    # print(book)
    # print(book.bpub_date)


    #      
    #              
    #                 
    # book=BookInfo.objects.filter(hero__hname='  ')
    # print(book)


    #             
    # book=BookInfo.objects.filter(hero__hname__contains=' ')
    # print(book)


    #          30         
    #hero=HeroInfo.objects.filter(hbook__bread__gt=30)
    #print(hero)
    return HttpResponse('    ')

좋은 웹페이지 즐겨찾기