简体   繁体   English

外键 Django 模型

[英]Foreign Key Django Model

I'm trying to create 3 models ;我正在尝试创建 3 个模型; Person , Address and Anniversy . PersonAddressAnniversy The plan is to have one address and one anniversy for each person.计划是为每个人设置一个地址和一个纪念日。 But each address and anniversy can have multiple persons.但是每个地址和纪念日可以有多个人。

So far I have the following, but I think the OneToMany(foreign key) relationships maybe the wrong way round.到目前为止,我有以下内容,但我认为OneToMany(foreign key)关系可能是错误的。 ie each address can have one person but each person can have multiple addresses.即每个地址可以有一个人,但每个人可以有多个地址。

from django.db import models

class Person(models.Model):
    name = models.CharField(max_length=50)
    birthday = models.DateField()

    def __unicode__(self):
        return u'%s' % (self.name)

class Address(models.Model):
    person = models.ForeignKey(Person)
    address = models.CharField(max_length=150)

    def __unicode__(self):
        return u'%s' % (self.address)

class Anniversy(models.Model):
    person = models.ForeignKey(Person)
    anniversy = models.DateField()

    def __unicode__(self):
        return u'%s' % (self.anniversy)

You create the relationships the other way around;您以相反的方式创建关系; add foreign keys to the Person type to create a Many-to-One relationship:将外键添加到Person类型以创建多对一关系:

class Person(models.Model):
    name = models.CharField(max_length=50)
    birthday = models.DateField()
    anniversary = models.ForeignKey(
        "Anniversary", on_delete=models.CASCADE)
    address = models.ForeignKey(
        "Address", on_delete=models.CASCADE)

class Address(models.Model):
    line1 = models.CharField(max_length=150)
    line2 = models.CharField(max_length=150)
    postalcode = models.CharField(max_length=10)
    city = models.CharField(max_length=150)
    country = models.CharField(max_length=150)

class Anniversary(models.Model):
    date = models.DateField()

I used string names for the other models so they can still be defined after, or you can define the Person model last.我为其他模型使用了字符串名称,因此它们仍然可以在之后定义,或者您可以最后定义Person模型。

Any one person can only be connected to one address and one anniversary, but addresses and anniversaries can be referenced from multiple Person entries.任何一个人只能连接到一个地址和一个周年纪念日,但地址和周年纪念日可以从多个Person条目中引用。

Anniversary and Address objects will be given a reverse, backwards relationship too; AnniversaryAddress对象也将被赋予反向、反向的关系; by default it'll be called person_set but you can configure a different name if you need to.默认情况下,它将被称为person_set ,但如果需要,您可以配置不同的名称。 See Following relationships "backward" in the queries documentation.请参阅查询文档中的“向后”关系

I would advise, it is slightly better practise to use string model references for ForeignKey relationships if utilising an app based approach to seperation of logical concerns .我建议,如果使用基于应用程序的方法来分离逻辑关注点,使用字符串模型引用来处理ForeignKey关系会稍微好一些。

So, expanding on Martijn Pieters' answer:因此,扩展 Martijn Pieters 的答案:

class Person(models.Model):
    name = models.CharField(max_length=50)
    birthday = models.DateField()
    anniversary = models.ForeignKey(
        'app_label.Anniversary', on_delete=models.CASCADE)
    address = models.ForeignKey(
        'app_label.Address', on_delete=models.CASCADE)

class Address(models.Model):
    line1 = models.CharField(max_length=150)
    line2 = models.CharField(max_length=150)
    postalcode = models.CharField(max_length=10)
    city = models.CharField(max_length=150)
    country = models.CharField(max_length=150)

class Anniversary(models.Model):
    date = models.DateField()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM