Models and databases 之二 关系

在关系数据库中设置表与表之间的关系

Many-to-one的关系

定义一个多对一的关系,用django.db.models.ForeignKey。像用其他字段类型意向方便。ForeignKey有一个必须的参数:此类关联的model

from diango.db import models
class Manufacturer(models.Model):
      pass
class Car(model.Model):
      manufacturer = models.ForeignKey(Manufacturer,on_delete=models.CASCADE)
>>>
一个与自身有多对一关系的对象
models.ForeignKey('self', on_delete=models.CASCADE)

可以通过Abstract base class与多个子类建立多对一关系
products/models.py

from django.db import models
class AbstractCar(models.Model):
      manufacturer = models.ForeignKey('Maufacturer',on_delete=models.CASCADE)
      class Meta:
                abstract = True

production/models.py

from django.db import models
from products.models import AbstractCar
class Manufacturer(models.Model):
    pass
class Car(AbstractCar):
    pass
class BENSCar(AbstractCar):
    pass
>>>Car.manufacturer will point to `production.Manufacturer` here.

ForeignKey还接受一些其他的参数,用于定义关系。

  • ForeignKey.on_delete
    当一个被引用的对象被删除的时候,Django会通过on_delete的参数模仿SQL的相关的操作
.CASCADE    当被引用的对象被删除的时候,此引用对象也要一并执行删除的操作
.PROTECT    禁止删除并产生一个异常(ProtectedError)
.SET_NULL   把外键设置为null,规划时此字段要设置可接受null
.SET_DEFAULT 把外键设置为默认值,规划是此字段要设置一个默认值

设置其他值 采用一个方法的形式  callable
from django.conf import settings
from django.contrib.auth import get_user_model from django.db import models
def get_sentinel_user():
return get_user_model().objects.get_or_create(username='deleted')[0]
class MyModel(models.Model): user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.SET(get_sentinel_user), )

.DO_NOTHING  什么也不做
  • ForeignKey.limit_choices_to
    限制字段可供选择的范围。参数是字典的形式或者一个Q对象
staff_member = models.ForeignKey(User,on_delete=models.CASCADE,limit_choices_to={'is_staff':True},)
限制了staff_member只有User的is_staff的字段可以作为外键关联的记录
  • ForeignKey.related_name
    关联对象反向引用描述符
    当一张表的多个字段指向同一张表时,会出错。系统无法知道,通过另外一张表,访问XXX_set属性访问到的是哪个属性。这时,我们就需要为每个字段定义一个related_name属性,另外一张表访问这个表时,就会根据related_name的值来得到各个属性了。

Many-to-many

定义一个Many-to-many关系使用ManyToManyField。ManyToManyField有一个比传参数。

from django.db import models
class Topping(models):
    pass
class Pizza(models.Model):
    topping = models.ManyToManyField(Topping)

However, sometimes you may need to associate data with the relationship between two models.
用两个模型的关系模型将此二者的数据联系起来
例如:音乐家和音乐乐团之间的关系。他们是多对多的关系,但是关于这个会员资格的一些信息需要一个额外的数据模型来解释。Django允许指定一个model来管理这种关系,设置一些字段在这个中间模型里。

from django.db import models
class Person(models.Model):
      name = models.CharField(max_length=128)
      def __str__(self):
            return self.name
class Group(models.Model):
       name = models.CharField(max_length=128)
       members = models.ManyToManyField(Person,through='Membership')
        def __str__(self):
            return self.name
class Membership(models.Model):
       person = models.ForeignKey(Person,on_delete=models.CASCADE)
        group = models.ForeignKey(Group,on_detele=models.CASCADE)
        date_joined = models.DateField()
        invite_reason = models.CharField(max_length=64)
>>>其中Membership就是中间model。中间model中ForeignKey把两个有关联的model联系起来
>>> ringo = Person.objects.create(name="Ringo Starr") >>> paul = Person.objects.create(name="Paul McCartney") >>> beatles = Group.objects.create(name="The Beatles") >>> m1 = Membership(person=ringo, group=beatles,
... date_joined=date(1962, 8, 16),
... invite_reason="Needed a new drummer.")
>>> m1.save()
>>> beatles.members.all()
]>
>>> ringo.group_set.all()
]>
>>> m2 = Membership.objects.create(person=paul, group=beatles,
... date_joined=date(1960, 8, 1),
... invite_reason="Wanted to form a band.")
>>> beatles.members.all()
, ]>
在有中间model的多对多的关系中,往常的add(),create(),set()不起作用
>>> # The following statements will not work
>>> beatles.members.add(john)
>>> beatles.members.create(name="George Harrison") 
>>> beatles.members.set([john, paul, ringo, george])
只能通过创建中间Model的实例的方式添加新的关系

在查询的时候,与普通的Many-to-Many的关系一样

# Find all the members of the Beatles that joined after 1 Jan 1961
>>> Person.objects.filter(
... group__name='The Beatles',
... membership__date_joined__gt=date(1961,1,1)) 

>>> ringos_membership = Membership.objects.get(group=beatles, person=ringo) 
>>> ringos_membership.date_joined
datetime.date(1962, 8, 16)
>>> ringos_membership.invite_reason
'Needed a new drummer.'

One-to-one relationships

用OneToOneField来定义一个一对一的关系,需要传递一个把绑定的model

from django.db import models
class Place(models.Model):
      name = models.CharField(max_length=50)
      address = models.CharField(max_length=80)
      def __str__(self):
          return "%s the place" % self.name
class Restaurant(model.Model):
       place = models.OneToOneField(Place,on_delete=models.CASCADE,primary_key=True,)
       serves_hot_dogs = models.BooleanField(default=False)
       serves_pizza = models.BooleanField(default=False)
        def __str__(self):
              return "%s the restaurant"% self.place.name
class Waiter(models.Model):
        restaurant = models.ForeignKey(Restaurant,on_delete=models.CASCADE)
        name = models.CharField(max_length=50)
        def __str__(self):
              return "%s the waiter at %s"%(self.name,self,restaurant)

p1 = Place(name='Demon Dogs',address='994 W. Fullerton')
p1.save()
p2=Place(name='Ace Hardware',address='1013 N. Ashland')
p2.save()
r = Restaurant(place=p1,serves_hot_dogs=True,serves_pizza=False)
r.save()
------
r.place

p1.restaurant   #由于p1与r关联起来了,可以通过p1.resaurant的方式找到p1关联的resaurant。


添加一个Waiter
w = r.waiter_set.create(name='Joe')

你可能感兴趣的:(Models and databases 之二 关系)