首页 > 代码库 > django ORM 操作
django ORM 操作
data models sample:
from django.db import models class Blog(models.Model): name = models.CharField(max_length=100) tagline = models.TextField() def __str__(self): # __unicode__ on Python 2 return self.name class Author(models.Model): name = models.CharField(max_length=50) email = models.EmailField() def __str__(self): # __unicode__ on Python 2 return self.name class Entry(models.Model): blog = models.ForeignKey(Blog) 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): # __unicode__ on Python 2 return self.headline
1.创建记录(object)
To represent database-table data in Python objects, Django uses an intuitive(直观的) system: A model class represents a database table, and an instance of that class represents a particular record in the database table.
>>> from blog.models import Blog >>> b = Blog(name=‘Beatles Blog‘, tagline=‘All the latest Beatles news.‘) >>> b.save()
This performs an INSERT
SQL statement behind the scenes. Django doesn’t hit the database until you explicitly call save()
.
The save()
method has no return value.
2.1 保存记录更改到数据库
>>> b5 = Blog.objects.get(pk=1) >>> b5.name = ‘New name‘ >>> b5.save()
2.2 保存对外键字段和多对多字段的修改
- update foreignkey fields
>>> from blog.models import Entry >>> entry = Entry.objects.get(pk=1) >>> cheese_blog = Blog.objects.get(name="Cheddar Talk") >>> entry.blog = cheese_blog >>> entry.save()
- update manytomany fields
add one record to ManyToManyField
>>> from blog.models import Author >>> joe = Author.objects.create(name="Joe") >>> entry.authors.add(joe)
add multiple records to ManyToManyField
>>> 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、查询表记录
3.1 查询表所有记录(objects):
>>> all_entries = Entry.objects.all() # all() 一个结果集(列表)包含所有数据库表的记录
3.2. 通filter查询特表的特定记录(objects)
filter(**kwargs)
Returns a new QuerySet
containing objects that match the given lookup parameters.
exclude(**kwargs)
Returns a new QuerySet
containing objects that do not match the given lookup parameters.
注意:filter返回的是一个结果集(列表)
1 Entry.objects.filter(pub_date__year=2006) 查询所有发布日期是2006的entry 2 Entry.objects.all().filter(pub_date__year=2006) # 等同上面语句 3 4 支持链式查询 5 6 >>> Entry.objects.filter( 7 ... headline__startswith=‘What‘ 8 ... ).exclude( 9 ... pub_date__gte=datetime.date.today() 10 ... ).filter( 11 ... pub_date__gte=datetime(2005, 1, 30) 12 ... ) 13 14 15 filter 查询是独立的,q2,q3,不影响q1的结果 16 17 >>> q1 = Entry.objects.filter(headline__startswith="What") 18 >>> q2 = q1.exclude(pub_date__gte=datetime.date.today()) 19 >>> q3 = q1.filter(pub_date__gte=datetime.date.today())
querysets 是懒惰的
>>> q = Entry.objects.filter(headline__startswith="What") >>> q = q.filter(pub_date__lte=datetime.date.today()) >>> q = q.exclude(body_text__icontains="food") >>> print(q) 前面三条语句并没有真正从数据库获取数据,直到第四条语句,我们去打印起结果时,才真正从数据库获取数据
3.3、通过get()方法查询单个记录(objects)
当我们明确知道只有一条记录满足我们的查询时,我们可以使用get(), >>> one_entry = Entry.objects.get(pk=1) 注意:在使用get时,如果只有有多条记录满足,django 将抛出MultipleObjectsReturned异常,而使用filter方法时,则返回多个记录的列表;而没有记录满足时,get()将抛出DoesNotExist;filter没有异常抛出,只返回空列表
大部分情况下,我们只需要使用 all()
, get()
, filter()
and exclude()
这几个方法进行查询,所有其它方法可以在这里查看,https://docs.djangoproject.com/en/1.9/ref/models/querysets/#queryset-api
3.4 限定的查询集
Entry.objects.all()[:5] # 返回entry的前5条记录,相当于select * from entry limit 5; Entry.objects.all()[5:10] # 返回第六到第十条记录,相当于 select * from entry limit 5 offset 5; Entry.objects.all()[-1] # 不支持负索引 Entry.objects.all()[:10:2] #指定切片的步长 Entry.objects.order_by(‘headline‘)[0] #entry 按headline 字段排序后,取第一条记录。相当于 select * from entry order by limit 1;
3.5 field lookups
Field lookups are how you specify the meat of an SQL WHERE
clause. They’re specified as keyword arguments to theQuerySet
methods filter()
, exclude()
and get()
.
语法格式: field__lookuptype=value (双下划线)
>>> Entry.objects.filter(pub_date__lte=‘2006-01-01‘) 相当于: SELECT * FROM blog_entry WHERE pub_date <= ‘2006-01-01‘; field 必须是models class 的 field的名称 除了双下划线这种格式,还有一种形式,外键字段名称接_id, Entry.objects.filter(blog_id=4) #查询所有属于 id = 4 的 blog 的entry对象
exact:
Entry.objects.get(headline__exact="Cat bites dog") #精确匹配查询;相当于:SELECT ... WHERE headline = ‘Cat bites dog‘;
Blog.objects.get(id=14) 相当于 Blog.objects.get(id__excat=14)iexact:
大小写不敏感的字符匹配
>>> Blog.objects.get(name__iexact="beatles blog")
Would match a Blog
titled "Beatles Blog"
, "beatles blog"
, or even "BeAtlES blOG"
.
contains 与同样有icontains:
模糊匹配
Entry.objects.get(headline__contains=‘Lennon‘) 相当于sql: SELECT ... WHERE headline LIKE ‘%Lennon%‘;
startswith
, endswith,i
startswith
, iendswith
所有field lookups,请参考https://docs.djangoproject.com/en/1.9/ref/models/querysets/#field-lookups
3.6 多表关联查询
Entry.objects.filter(blog__name=‘Beatles Blog‘) #This example retrieves all Entry objects with a Blog whose name is ‘Beatles Blog‘:
相当于sql:
也支持反向操作: Blog.objects.filter(entry__headline__contains=‘Lennon‘) #This example retrieves allBlog
objects which have at least oneEntry
whoseheadline
contains‘Lennon‘
:
相当于sql:
django ORM 操作