Day15-Django

 1 all_entries = Entry.objects.all() #查询所有
 2 Entry.objects.filter(pub_date__year=2006) #查询所有pub_date为2006年的纪录
 3 Entry.objects.all().filter(pub_date__year=2006) #与上面那句一样
 4 >>> Entry.objects.filter(   #链式查询
 5 ...     headline__startswith='What'
 6 ... ).exclude(
 7 ...     pub_date__gte=datetime.date.today()
 8 ... ).filter(
 9 ...     pub_date__gte=datetime(2005, 1, 30)
10 ... )
11 
12 one_entry = Entry.objects.get(pk=1) #单条查询
13 
14 Entry.objects.all()[:5] #查询前5条
15 Entry.objects.all()[5:10] #你猜
16 
17 Entry.objects.order_by('headline')[0] #按headline排序取第一条
18 
19 Entry.objects.filter(pub_date__lte='2006-01-01') #相当于sql语句SELECT * FROM blog_entry WHERE pub_date <= '2006-01-01';
20 
21 Entry.objects.get(headline__exact="Cat bites dog") #相当于SELECT ... WHERE headline = 'Cat bites dog';
22 Blog.objects.get(name__iexact="beatles blog") #与上面相同,只是大小写不敏感
23 
24 Entry.objects.get(headline__contains='Lennon') #相当 于SELECT ... WHERE headline LIKE '%Lennon%';
原文地址:https://www.cnblogs.com/kiko0o0/p/10636427.html