1. 程式人生 > >djang orm的查詢

djang orm的查詢

django的查詢 
        books = Book.objects.filter(id=2)
        books = Book.objects.exclude(name="python").values("name","price")#排除條件

        books = Book.objects.all()
        books = Book.objects.all()[::2]
        books = Book.objects.all()[::-1]
        
        #first last get取到的都是例項物件,並非一個QuerySet集合物件
        books = Book.objects.first()
        books = Book.objects.last()
        books = Book.objects.get(id=2)#只能取一條記錄時才不報錯
        
        ret1 = Book.objects.first(name="python").values("name")#篩選
        ret2 = Book.objects.first(name="python").values_list("name","price")## 可以獲取number、price的元組列表,而不是字典
        

        books = Book.objects.all().values("name").distinct()#去重
        books = Book.objects.all().values("name").distinct().count()#欄位name去重後的行數