Django-如何规范化数据库? [英] Django - how to normalize database?

查看:65
本文介绍了Django-如何规范化数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在问人们我应该如何组织我的模型,他们总是告诉我对数据库进行规范化。

I keep asking people how I should organize my model, and they keep telling me to normalize the database.

有人可以向我展示规范化Django模型的示例吗? ?

Can someone show me an example of a normalized Django model?

推荐答案

规范化不是Django甚至Python的概念-它是设计关系数据库架构的更广泛方法,因此您可以消除重复并消除冗余。

Normalisation is not a Django or even Python concept - it is a wider approach to designing a relational database schema such that you remove duplication and eliminate redundancy.

在Django中,这意味着您可能有多个较小的模型,而不是只有一个模型,这些模型通过 ForeignKey ManyToMany 字段模型API

In django this means that rather than have one model, you might have multiple smaller models, which represent the relationship between database tables via ForeignKey and ManyToMany fields provided by the Model API.

因此,请使用从 django文档,例如,我们在一家报纸公司工作,我们希望跟踪每位记者及其发表的文章。您可能会首先设计这样的模型

So using an example contrived from the django docs, say we work for a newspaper company and we want to keep track of each reporter and the articles they publish. You might design a model like this first off

class Newspaper(models.Model):
    reporter = models.CharField(max_length=30)
    email = models.EmailField()
    headline = models.CharField(max_length=100)

如果我们填充此模型,那么当记者发表多篇文章时,我们将有冗余,因为我们将复制电子邮件信息,并将相同的电子邮件地址存储在多行中。这是一个问题,因为将来如果我们要更新记者的电子邮件地址,我们可能会错过一行并破坏我们的数据。

If we populate this model, we are going to have redundancy as soon as a reporter publishes more than one article, as we will be duplicating email information, storing the same email address in multiple rows. This is a problem, as in the future if we want to update a reporters email address, we might a miss row and corrupt our data.

因此我们可以开始进行改进在此基础上,开始通过定义两个单独的表来规范化我们的模型,这些表使用 ForeignKey 字段映射多对一关系(又称记者可以发表许多文章,但一篇文章只有一个记者)。

So we could start to improve on this and begin to normalise our model by defining two separate tables, which use a ForeignKey field to map the many-to-one relationship (aka that a reporter can publish many articles, but an article only has one reporter).

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

class Article(models.Model):
    headline = models.CharField(max_length=100)
    reporter = models.ForeignKey(Reporter)

现在,我们不会复制记者的电子邮件地址,因为我们可以将此ONCE存储在Reporter表中,以便将来更轻松地更新此值。

Now we won't be duplicating the reporters email address, as we can store this ONCE in the Reporter table, making it easier to update this value in the future.

现在,这是一个有些人为的答案,我敢肯定您可以进一步改善这种设计点火,但希望它能证明这一点。我真的建议您阅读 django文档,其中讨论了关系建模各种标准化程度

Now this is a somewhat contrived answer, and I'm sure you could go further to improve this design, but hopefully it demonstrates the point. I really recommend you read the django docs which talk about the modeling of relationships and the various degrees of normalisation.

这篇关于Django-如何规范化数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆