Python 外键 Django 模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14663523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:06:27  来源:igfitidea点击:

Foreign Key Django Model

pythondjangodjango-models

提问by felix001

I'm trying to create 3 models ; Person, Addressand Anniversy. The plan is to have one address and one anniversy for each person. But each address and anniversy can have multiple persons.

我正在尝试创建 3 个模型;PersonAddressAnniversy。计划是每个人有一个地址和一个纪念日。但是每个地址和纪念日可以有多个人。

So far I have the following, but I think the OneToMany(foreign key)relationships maybe the wrong way round. i.e each address can have one person but each person can have multiple addresses.

到目前为止,我有以下几点,但我认为这些OneToMany(foreign key)关系可能是错误的。即每个地址可以有一个人,但每个人可以有多个地址。

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)

采纳答案by Martijn Pieters

You create the relationships the other way around; add foreign keys to the Persontype 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()

Any one person can only be connected to oneaddress and oneanniversary, but addresses and anniversaries can be referenced from multiple Personentries.

任何一个人只能连接到一个地址和一个周年纪念日,但地址和周年纪念日可以从多个Person条目中引用。

Anniversaryand Addressobjects will be given a reverse, backwards relationship too; by default it'll be called person_setbut you can configure a different name if you need to. See Following relationships "backward"in the queries documentation.

Anniversary并且Address对象也将被赋予反向的、向后的关系;默认情况下会调用它,person_set但如果需要,您可以配置不同的名称。请参阅查询文档中的以下关系“向后”

回答by Micheal J. Roberts

I would advise, it is slightly better practise to use string model references for ForeignKeyrelationships 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()