class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __str__(self): return self.name class Author(models.Model): name = models.CharField(max_length=200) email = models.EmailField() def __str__(self): return self.name class Entry(models.Model): blog = models.ForeignKey(Blog, on_delete=models.CASCADE) headline = models.CharField(max_length=255) body_text = models.TextField() pub_date = models.DateField() mod_date = models.DateField() authors = models.ManyToManyField(Author) n_comments = models.IntegerField() n_pingbacks = models.IntegerField() rating = models.IntegerField() def __str__(self): return self.headline
1,一对多关系的添加
>>> entry = Entry.objects.get(pk=1) >>> cheese_blog = Blog.objects.get(name="Cheddar Talk") >>> entry.blog = cheese_blog >>> entry.save()
2,多对多的关系的添加
>>> john = Author.objects.create(name="John") >>> paul = Author.objects.create(name="Paul") >>> george = Author.objects.create(name="George") >>> ringo = Author.objects.create(name="Ringo") >>> entry.authors.add(john, paul, george, ringo)
3,链接式过滤查询子集
>>> Entry.objects.filter( ... headline__startswith='What' ... ).exclude( ... pub_date__gte=datetime.date.today() ... ).filter( ... pub_date__gte=datetime.date(2005, 1, 30) ... )
4,get请求
one_entry = Entry.objects.get(pk=1)当get请求访问时,如果没有当前数据,那么会报DoesNotExist的错误。在实际应用中可以,try捕捉异常.ps:如果get请求捕捉到多个值,那么也会报错,错误为MultipleObjectsReturned,
二,查找方式 PS:exact,startwith,endwith,contains 等查找只要前面加上i,那么大小写是一样的。
1,exact 精确查找 Entry.objects.get(headline__exact="Cat bites dog")2,iexact 只要字母对,大小写不影响结果
Entry.objects.get(headline__iexact="Cat bites dog")3,isnull 是否为空
F参数
>>> from django.db.models import F >>> Entry.objects.filter(n_comments__gt=F('n_pingbacks')) >>> Entry.objects.filter(n_comments__gt=F('n_pingbacks') * 2) >>> Entry.objects.filter(rating__lt=F('n_comments') + F('n_pingbacks'))
Entry.objects.filter(authors__name=F('blog__name'))
>>> from datetime import timedelta >>> Entry.objects.filter(mod_date__gt=F('pub_date') + timedelta(days=3))
Q参数
Q(question__startswith='Who') | Q(question__startswith='What')
Q(question__startswith='Who') | ~Q(pub_date__year=2005) ~表示的是非
Poll.objects.get( Q(question__startswith='Who'), Q(pub_date=date(2005, 5, 2)) | Q(pub_date=date(2005, 5, 6)) 这里的Q表示and )
三,queryset缓存 print的时候:不会调用缓存,所以每次print都会引起数据库的再次调用
>>> queryset = Entry.objects.all() >>> print([p.headline for p in queryset]) # Evaluate the query set. >>> print([p.pub_date for p in queryset]) # Re-use the cache from the evaluation.切片会引起queryset的再次操作
>>> queryset = Entry.objects.all() >>> print(queryset[5]) # Queries the database >>> print(queryset[5]) # Queries the database again如果先遍历后,那么后续切片就会有限运用缓存
>>> queryset = Entry.objects.all() >>> [entry for entry in queryset] # Queries the database >>> print(queryset[5]) # Uses cache >>> print(queryset[5]) # Uses cache
下面的例子也是应用缓存
>>> [entry for entry in queryset] >>> bool(queryset) >>> entry in queryset >>> list(queryset)
四,删除delete
e.delete() Entry.objects.filter(pub_date__year=2005).delete() b = Blog.objects.get(pk=1) # This will delete the Blog and all of its Entry objects. b.delete() Entry.objects.all().delete()
五,更新update
Entry.objects.filter(pub_date__year=2007).update(headline='Everything is the same')
>>> b = Blog.objects.get(pk=1) # Change every Entry so that it belongs to this Blog. >>> Entry.objects.all().update(blog=b)
来源:https://www.cnblogs.com/52forjie/p/9636570.html