應用場景:python
#!/usr/bin/env python# -*- coding: utf-8 -*-from __future__ import unicode_literalsfrom django.db import modelsclass Author(models.Model):name = models.CharField(max_length=30)class Publisher(models.Model):name = models.CharField(max_length=50)class Book(models.Model):name = models.CharField(max_length=50)#一本書由一家出版社發佈,一個出版社發佈多本書。屬於一對多關係,用ForeignKey()pub = models.ForeignKey(Publisher)#一本書能夠由多個做者合寫,一個做者能夠寫多本書,屬於多對多關係,用ManyToManyFieldauthors = models.ManyToMany(Author)
生成結果:
一共生成了4張表:
web_author(做者表)
web_publisher(出版社表)
web_book_authors(記錄book與author多對多關係表。多對多關係要藉助第三張表創建關係)
web_book(book表,其中pub_id體現書與出版社之間的一對多關係)
3. 表關係進階web
1. 關聯還沒有定義的Model
class Book(models.Model):name = models.CharField(max_length = 50)#若是Publisher與Author在Book後面定義,須要使用model 的名稱,而不是使用 model 對象自己pub = models.ForeignKey('Publisher')authors = models.ManyToManyField('Author')class Publisher(models.Model):name = models. CharField (max_length = 50)class Author(models.Model):name = models.CharField(max_length = 30)
2. Model關聯自身
1) Model能夠與自身作多對一關係
class People(models.Model):name = models.CharField(max_length = 30)leader = models.ForeignKey('self', blank=True, null=True)
說明:一個領導有多個下屬,一個下屬對應一個直接領導,同時領導也是領導的下屬。就屬於多對一關係,且須要與自身作多對一關係。且注 意,設計這表時要設置blank=True和null=True.2) Model能夠與自身作多對多關係
class Person(models.Model):friends = models.ManyToManyField('self')
用來定義一對一關係。籠統地講,它與聲明瞭 unique=True 的 ForeignKey 很是類似,不一樣的是使用反向關聯的時候,獲得的不是一個對象列表,而是一個單獨的對象。在某個 model 擴展自另外一個 model 時,這個字段是很是有用的;例如: 多表繼承 (Multi-tableinheritance) 就是經過在子 model 中添加一個指向父 model 的一對一關聯而實現的。必須給該字段一個參數:被關聯的 model 類。工做方式和 ForeignKey 同樣,連遞歸關聯 (recursive) 和 延後關聯 (lazy) 都同樣。 此外,OneToOneField 接受 ForeignKey 可接受的參數,只有一個參數是 OnetoOneField 專有的:OneToOneField.parent_link, 若是爲 True,而且做用於繼承自某個父 model 的子 model 上(這裏不能是延後繼承,父 model 必須真實存在 ),那麼該字段就會變成指向父類實例的引用(或者叫連接), 而不是象其餘OneToOneField 那樣用於擴展父類並繼承父類屬性。
from django.db import models, transaction, IntegrityErrorclass Place(models.Model):name = models.CharField(max_length=50)address = models.CharField(max_length=80)def __unicode__(self):return u"%s the place" % self.nameclass Restaurant(models.Model):place = models.OneToOneField(Place, primary_key=True)serves_hot_dogs = models.BooleanField()serves_pizza = models.BooleanField()def __unicode__(self):return u"%s the restaurant" % self.place.nameclass Waiter(models.Model):restaurant = models.ForeignKey(Restaurant)name = models.CharField(max_length=50)def __unicode__(self):return u"%s the waiter at %s" % (self.name, self.restaurant)
>>> p1 = Place(name='Demon Dogs', address='944 W. Fullerton')>>> p1.save()>>> r = Restaurant(place=p1, serves_hot_dogs=True, serves_pizza=False)>>> r.save()>>> p1.restaurant<Restaurant: Demon Dogs the restaurant>>>> Place.objects.get(restaurant__place__name__startswith="Demon")<Place: Demon Dogs the place>>>> Waiter.objects.filter(restaurant__place__name__startswith="Demon")