標籤:
第三篇最後寫了,光知道那些基礎的查詢在項目中是沒有什麼卵用的,重點是實體關聯映射(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.name14 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.title44 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=‘[email protected]‘,detail=AuthorInfo.objects.get(id=1))11 >>>Author.objects.create(frist_name=‘Susan‘,last_name=‘Jeerry‘,email=‘[email protected]‘,detail=AuthorInfo.objects.get(id=2))12 >>>Author.objects.create(frist_name=‘Jerry‘,last_name=‘Brith‘,email=‘[email protected]‘,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.detail3 <AuthorInfo:this gender:1>
也可以反向根據使用者詳細資料查詢使用者基本資料:
1 >>>d = AuthorInfo.objects.get(id=1)2 >>>a = d.author3 >>>a4 <Author:Jack Jeeb>
這裡注意反向查詢要使用表關聯的對象的小寫類名
一對多ForeignKey:
通過書籍資訊查詢所屬出版社資訊:
1 >>>b = Book.objects.get(id=1)2 >>>b.publisher3 <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