简体   繁体   English

我应该使用哪种类型的数据库关系?

[英]What type of database relationship should I use?

from django.db import models

class Reporter(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()

    def __str__(self):
        return "%s %s" % (self.first_name, self.last_name)

class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)

    def __str__(self):
        return self.headline

    class Meta:
        ordering = ('headline',)

I found this one-to-many relationship example in the Django docs. 我在Django文档中找到了此一对多关系示例。 How would I go about doing another class called Magazine. 我将如何去做另一个叫做Magazine的课程。 In the class magazine, it would contain many articles written by many reporters. 在班级杂志中,它将包含许多记者撰写的许多文章。 Would I still use a many-to-one relationship, or would I need a one-to-one relationship? 我是否仍将使用多对一关系,还是需要一对一关系?

That really depends on what you want to do... You have to be more specific, and tell us what your problem really is. 那真的取决于您要做什么...您必须更具体,然后告诉我们您的问题到底是什么。

  1. If you have n Articles in your Magazine, and each Article only occur in one Magazine, you have to use one-to-many. 如果您的杂志中有n篇文章,而每篇文章仅出现在一本杂志中,则您必须使用一对多。

  2. If the Article can occur in multiple magazines, you have to use many-to-many relationship. 如果文章可以出现在多个杂志上,则必须使用多对多关系。

That's not actually a python question, by the way... 顺便说一句,这实际上不是python问题。

This is either a ForeignKey from Article to Magazine , or a ManyToManyField between Article and Magazine . 这是ArticleMagazineForeignKey ,或者是ArticleMagazine之间ManyToManyField This depends on whether an article can occur in multiple magazines (frequently journalists sell the same article to different magazines, those are then, except for some formatting, word-by-word the same). 这取决于一篇文章是否可以在多个杂志上发表(记者经常将一篇文章卖给不同的杂志,然后,除了某些格式外,都是逐字逐句的)。

In case every article occurs in one magazine, then we thus implement it like: 如果每一篇文章都出现在一个杂志上,那么我们可以这样实现:

class Article(models.Model):
    headline = models.CharField(max_length=100)
    pub_date = models.DateField()
    reporter = models.ForeignKey(Reporter, on_delete=models.CASCADE)
    magazine = models.ForeignKey(Magazine, on_delete=models.CASCADE)

    def __str__(self):
        return self.headline

    class Meta:
        ordering = ('headline',)

In case an article can remain unpublished , then you also probably have to set null=True , in the ForeignKey constructor. 如果文章可以保持未发布状态 ,那么您可能还必须在ForeignKey构造函数中设置null=True

In case of a ManyToManyField , django will implicitly create a table like article_magazine that stores mappings from Article s to Magazine s (but if you query, you will only obtain the Magazine s). 对于ManyToManyField ,django将隐式创建一个表,例如article_magazine ,该表存储从ArticleMagazine的映射(但是如果您查询,则只会获取Magazine )。

The relation is very probably not a OneToOneField . 该关系很可能不是 OneToOneField In fact a OneToOneField is a ForeignKey with unique=True . 实际上, OneToOneField是具有unique=TrueForeignKey This thus means that no two Article s are published in the same Magazine . 因此,这意味着, 没有两个Article s的发表在同一 Magazine Therefore it means that every Magazine has either no or exactly one Article related to it. 因此,这意味着每本Magazine都没有或只有一篇与之相关的Article

According to your need i think you should refer to Many-to-many relationship . 根据您的需要,我认为您应参考多对多关系

  1. In you think, an Article can be published using multiple reporter objects, and a Reporter has multiple Article objects then refer to https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_many/ 您认为,可以使用多个报告者对象发布文章,并且报告者可以有多个文章对象,然后参考https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_many/

  2. If you Logic supports one reporter per article and multiple article per reporter then visit: https://docs.djangoproject.com/en/2.0/topics/db/examples/many_to_one/ . 如果您的Logic每篇文章支持一位记者,每篇报道支持多篇文章,请访问: https : //docs.djangoproject.com/en/2.0/topics/db/examples/many_to_one/

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

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