第三篇最後寫了,光知道那些基礎的查詢在項目中是沒有什麼卵用的,重點是實體關係映射(ORM),今天學習了,來記錄一下,關鍵詞:ForeignKey(多對一)、OneToOneField(一對一)、ManyToManyField(多對多)python
實體定義:web
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中簡單拖了個模型:shell
一個出版社對應多本書,Book中經過外鍵ForeignKey來指定,一本書有多個做者,一個做者也會有多本書,因此這裏是多對多關係(ManyToManyField),一個做者Author又會有詳細信息,這裏應該是一對一的關係(OneToOneField),最下面的表book_authors是Book實體中定義的ManyToManyField字段自動生成的,下面來插入數據django
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,下面看下查詢的方法asp.net
一對一OneToOneField:學習
經過用戶查詢用戶詳細信息:this
1 >>>a = Author.objects.get(id=1) 2 >>>d = a.detail 3 <AuthorInfo:this gender:1>
也能夠反向根據用戶詳細信息查詢用戶基本信息:spa
1 >>>d = AuthorInfo.objects.get(id=1) 2 >>>a = d.author 3 >>>a 4 <Author:Jack Jeeb>
這裏注意反向查詢要使用表關聯的對象的小寫類名.net
一對多ForeignKey:code
經過書籍信息查詢所屬出版社信息:
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