码迷,mamicode.com
首页 > 编程语言 > 详细

Python Django 开发 4 ORM

时间:2015-06-30 18:12:58      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:

第三篇最后写了,光知道那些基础的查询在项目中是没有什么卵用的,重点是实体关系映射(ORM),今天学习了,来记录一下,关键词:ForeignKey(多对一)、OneToOneField(一对一)、ManyToManyField(多对多)

实体定义:

 1 from django.db import models
 2 
 3 # Create your models here.
 4 class Publisher(models.Model):
 5     name = models.CharField(max_length=30)
 6     address = models.CharField(max_length=50)
 7     city = models.CharField(max_length=60)
 8     state_province = models.CharField(max_length=30)
 9     county = models.CharField(max_length=50)
10     website = models.URLField()
11 
12     def __str__(self):
13         return self.name
14 
15 class AuthorInfo(models.Model):
16     gender = models.IntegerField()
17     brithday = models.DateField()
18     join_time = models.DateTimeField()
19 
20     def __str__(self):
21         return this gender:+ str(self.gender)
22 
23 
24 class Author(models.Model):
25     frist_name = models.CharField(max_length=30)
26     last_name = models.CharField(max_length=40)
27     email = models.EmailField()
28     detail = models.OneToOneField(AuthorInfo)
29 
30     def __str__(self):
31         return u%s %s % (self.frist_name,self.last_name)
32 
33 
34 
35 
36 class Book(models.Model):
37     title = models.CharField(max_length=100)
38     authors = models.ManyToManyField(Author)
39     publisher = models.ForeignKey(Publisher)
40     publication_date=models.DateField()
41 
42     def __str__(self):
43         return self.title
44 
45     

在Navicat中简单拖了个模型:

技术分享

一个出版社对应多本书,Book中通过外键ForeignKey来指定,一本书有多个作者,一个作者也会有多本书,所以这里是多对多关系(ManyToManyField),一个作者Author又会有详细信息,这里应该是一对一的关系(OneToOneField),最下面的表book_authors是Book实体中定义的ManyToManyField字段自动生成的,下面来插入数据

 1 #python shell 下
 2 >>>Publisher.objects.create(name=cbs1,address=xxx,city=yyy,state_province=zzz,county=China,website=Http://www.do-iot.net)
 3 >>>Publisher.objects.create(name=cbs2,address=xxx,city=yyy,state_province=zzz,county=China,website=Http://www.do-iot.net)
 4 >>>Publisher.objects.create(name=cbs3,address=xxx,city=yyy,state_province=zzz,county=China,website=Http://www.do-iot.net)
 5 
 6 >>>AuthorInfo.objects.create(gender=1,brithday=2012-03-12,join_time=2013-03-04 12:21:32)
 7 >>>AuthorInfo.objects.create(gender=0,brithday=2012-03-12,join_time=2013-03-04 12:21:32)
 8 >>>AuthorInfo.objects.create(gender=1,brithday=2012-03-12,join_time=2013-03-04 12:21:32)
 9 
10 >>>Author.objects.create(frist_name=John,last_name=Leb,email=litdev@outlook.com,detail=AuthorInfo.objects.get(id=1))
11 >>>Author.objects.create(frist_name=Susan,last_name=Jeerry,email=litdev@qq.com,detail=AuthorInfo.objects.get(id=2))
12 >>>Author.objects.create(frist_name=Jerry,last_name=Brith,email=litdev@outlook.com,detail=AuthorInfo.objects.get(id=3))
13 
14 >>>b = Book.objects.create(title=book1,publisher=Publisher.objects.get(id=1),publication_date=2015-06-30)
15 >>>b.authors.add(Author.objects.get(id=1))
16 >>>b = Book.objects.create(title=book2,publisher=Publisher.objects.get(id=2),publication_date=2015-06-30)
17 >>>b.authors.add(Author.objects.get(id=2))
18 >>>b = Book.objects.create(title=book2,publisher=Publisher.objects.get(id=3),publication_date=2015-06-30)
19 >>>b.authors.add(Author.objects.get(id=3))

一对一和一对多添加的方式差不多,多对多的需要先添加除ManyToManyField字段的数据,再在这个实体上添加ManyToManyField关联的对象,原先我数据都插入好了,这里写的都是现场手打的数据,可能会有bug,下面看下查询的方法

一对一OneToOneField:

通过用户查询用户详细信息:

1 >>>a = Author.objects.get(id=1)
2 >>>d = a.detail
3 <AuthorInfo:this gender:1>

也可以反向根据用户详细信息查询用户基本信息:

1 >>>d = AuthorInfo.objects.get(id=1)
2 >>>a = d.author
3 >>>a
4 <Author:Jack Jeeb>

这里注意反向查询要使用表关联的对象的小写类名

一对多ForeignKey:

通过书籍信息查询所属出版社信息:

1 >>>b = Book.objects.get(id=1)
2 >>>b.publisher
3 <Publisher:cbs1>

反向根据出版社查询所有出版的书:

1 >>>p = Publisher.objects.get(id=1)
2 >>>b_list = p.book_set.all()
3 >>>len(b_list)
4 2

注意这里查询使用小写关联的类名+"_set"来查询,后面的all()就跟普通的查询一样了,where、order_by等等

多对多ManyToManyField:

根据书籍查询作者:

1 >>>b = Book.objects.get(id=1)
2 >>>b.authors.all()
3 [<Author: Jack Jeeb>, <Author: Susan leb>]

反向根据作者查询书籍:

1 >>>a = Author.objects.get(id=1)
2 >>>a.book_set.all()
3 [<Book:book1>]

跟一对多反向查询差不多

理解这些一一般项目数据层都没有问题了(经验、毕竟是做了两三年的asp.net开发的大水B)

 

PS参考资料:

http://logic0.blog.163.com/blog/static/18892814620137343447299/

http://blog.csdn.net/fengyu09/article/details/17434795

Python Django 开发 4 ORM

标签:

原文地址:http://www.cnblogs.com/New-world/p/4611089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!