第三篇最后写了,光知道那些基础的查询在项目中是没有什么卵用的,重点是实体关系映射(ORM),今天学习了,来记录一下,关键词:ForeignKey(多对一)、OneToOneField(一对一)、ManyToManyField(多对多)
实体定义:
from django.db import models # Create your models here.
class Publisher(models.Model):
name = models.CharField(max_length=30)
address = models.CharField(max_length=50)
city = models.CharField(max_length=60)
state_province = models.CharField(max_length=30)
county = models.CharField(max_length=50)
website = models.URLField() def __str__(self):
return self.name class AuthorInfo(models.Model):
gender = models.IntegerField()
brithday = models.DateField()
join_time = models.DateTimeField() def __str__(self):
return 'this gender:'+ str(self.gender) class Author(models.Model):
frist_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
email = models.EmailField()
detail = models.OneToOneField(AuthorInfo) def __str__(self):
return u'%s %s' % (self.frist_name,self.last_name) class Book(models.Model):
title = models.CharField(max_length=100)
authors = models.ManyToManyField(Author)
publisher = models.ForeignKey(Publisher)
publication_date=models.DateField() def __str__(self):
return self.title
在Navicat中简单拖了个模型:
一个出版社对应多本书,Book中通过外键ForeignKey来指定,一本书有多个作者,一个作者也会有多本书,所以这里是多对多关系(ManyToManyField),一个作者Author又会有详细信息,这里应该是一对一的关系(OneToOneField),最下面的表book_authors是Book实体中定义的ManyToManyField字段自动生成的,下面来插入数据
#python shell 下
>>>Publisher.objects.create(name='cbs1',address='xxx',city='yyy',state_province='zzz',county='China',website='Http://www.do-iot.net')
>>>Publisher.objects.create(name='cbs2',address='xxx',city='yyy',state_province='zzz',county='China',website='Http://www.do-iot.net')
>>>Publisher.objects.create(name='cbs3',address='xxx',city='yyy',state_province='zzz',county='China',website='Http://www.do-iot.net') >>>AuthorInfo.objects.create(gender=1,brithday='2012-03-12',join_time='2013-03-04 12:21:32')
>>>AuthorInfo.objects.create(gender=0,brithday='2012-03-12',join_time='2013-03-04 12:21:32')
>>>AuthorInfo.objects.create(gender=1,brithday='2012-03-12',join_time='2013-03-04 12:21:32') >>>Author.objects.create(frist_name='John',last_name='Leb',email='litdev@outlook.com',detail=AuthorInfo.objects.get(id=1))
>>>Author.objects.create(frist_name='Susan',last_name='Jeerry',email='litdev@qq.com',detail=AuthorInfo.objects.get(id=2))
>>>Author.objects.create(frist_name='Jerry',last_name='Brith',email='litdev@outlook.com',detail=AuthorInfo.objects.get(id=3)) >>>b = Book.objects.create(title='book1',publisher=Publisher.objects.get(id=1),publication_date='2015-06-30')
>>>b.authors.add(Author.objects.get(id=1))
>>>b = Book.objects.create(title='book2',publisher=Publisher.objects.get(id=2),publication_date='2015-06-30')
>>>b.authors.add(Author.objects.get(id=2))
>>>b = Book.objects.create(title='book2',publisher=Publisher.objects.get(id=3),publication_date='2015-06-30')
>>>b.authors.add(Author.objects.get(id=3))
一对一和一对多添加的方式差不多,多对多的需要先添加除ManyToManyField字段的数据,再在这个实体上添加ManyToManyField关联的对象,原先我数据都插入好了,这里写的都是现场手打的数据,可能会有bug,下面看下查询的方法
一对一OneToOneField:
通过用户查询用户详细信息:
>>>a = Author.objects.get(id=1)
>>>d = a.detail
<AuthorInfo:this gender:1>
也可以反向根据用户详细信息查询用户基本信息:
>>>d = AuthorInfo.objects.get(id=1)
>>>a = d.author
>>>a
<Author:Jack Jeeb>
这里注意反向查询要使用表关联的对象的小写类名
一对多ForeignKey:
通过书籍信息查询所属出版社信息:
>>>b = Book.objects.get(id=1)
>>>b.publisher
<Publisher:cbs1>
反向根据出版社查询所有出版的书:
>>>p = Publisher.objects.get(id=1)
>>>b_list = p.book_set.all()
>>>len(b_list)
2
注意这里查询使用小写关联的类名+"_set"来查询,后面的all()就跟普通的查询一样了,where、order_by等等
多对多ManyToManyField:
根据书籍查询作者:
>>>b = Book.objects.get(id=1)
>>>b.authors.all()
[<Author: Jack Jeeb>, <Author: Susan leb>]
反向根据作者查询书籍:
>>>a = Author.objects.get(id=1)
>>>a.book_set.all()
[<Book:book1>]
跟一对多反向查询差不多
理解这些一一般项目数据层都没有问题了(经验、毕竟是做了两三年的asp.net开发的大水B)
PS参考资料:
http://logic0.blog.163.com/blog/static/18892814620137343447299/
http://blog.csdn.net/fengyu09/article/details/17434795