django之间的区别 - 一对一,多对一和多对多 [英] django difference between - one to one, many to one and many to many

查看:20
本文介绍了django之间的区别 - 一对一,多对一和多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这是我第一次学习计算机语言.我选择了python和django.现在,我了解了 python 和 django 的许多基本概念.我可以使用视图和所有其他内容创建新页面.但我仍然对这些关系感到困惑,即一对一、多对一和多对多.请有人请向我解释一下.我怎样才能使用它?我真的需要知道.

So, this is my first time learning computer language. And I chose python and django. Now, I got many of the basic concepts of python and also django. I can create new page with the views and all other stuff. But I am still confused with the relations, i.e. one to one, many to one, and many to many. Will someone please please please explain it to me. How can I use it? I really need to know.

谢谢.

推荐答案

特别是 django,这让复杂的 db 设计变得轻而易举,我认为了解它们在 SQL 级别的工作方式非常重要,以便更好地理解您的任何内容是做.我认为一个例子是理解这一点的最好方式.

Especially with django, which makes complicated db design a piece of cake, I think it's very important to understand how they work on the SQL level to better understand anything you are doing. I think an example is the best way to understand this.

您应该了解的第一件事是每个 SQL 表都有一个字段(通常会自动递增),称为主键".该字段是每一行具有唯一值的列.

First thing you should understand is that each SQL table has one field (which is usually automatically incremented) which is called a 'primary-key'. This field is a column with a unique value for each row.

假设在 Django 中,您创建了一个代表作者的模型,该模型具有三个字段 - first_name、last_name 和一个包含电子邮件的可选字段.Django 还会自动添加主键字段并将其命名为 pk(您也可以决定定义自己的字段以用作主键,但通常不这样做).因此,当使用命令 manage.py syncdb 时,它将创建一个如下所示的表:

Say in django you create a model representing an author, which has three fields - first_name, last_name and an optional field containing email. Django will also automatically add the primary-key field and call it pk (you can also decide to define your own field to use as primary key but usually don't). So when using the command manage.py syncdb it will create a table that looks like this:

+----+------------+-----------+-----------------------+
| pk | first_name | last_name |         email         |
+----+------------+-----------+-----------------------+

当你添加一个新值(比如Stephen King")时,它会像这样将它添加到作者表中:

When you add a new value (say 'Stephen King') it would add it to the authors table like so:

+----+------------+-----------+-----------------------+
| pk | first_name | last_name |         email         |
+----+------------+-----------+-----------------------+
|  1 | Stephen    | King      | stephenking@gmail.com |
+----+------------+-----------+-----------------------+

让我们再添加一个:

+----+------------+-----------+-----------------------+
| pk | first_name | last_name |         email         |
+----+------------+-----------+-----------------------+
|  1 | Stephen    | King      | stephenking@gmail.com |
|  2 | J.D.       | Salinger  |                       |
+----+------------+-----------+-----------------------+

这很简单.现在我们添加一个名为 Book 的新模型:

That's simple. Now we add a new model called Book:

+----+--------------+--------+--------+
| pk |    title     | genre  | author |
+----+--------------+--------+--------+
|  1 | Pet Semetary | Horror |      1 |
+----+--------------+--------+--------+

现在看看我在那里做了什么?在作者字段中,我为书提供了斯蒂芬金主键的值——记住,它是唯一的,所以它只会取回斯蒂芬金.这是一个 ForeignKey - 它指向相关表上的一个 pk,并代表一种多对一关系,即各种书籍可以指向一个作者的同一个 pk,但反过来不行.这样每个作者可以有很多相关的书,但每本书只有一个作者.

Now see what I did there? at the field for author I gave book the value for Stephen King's primary key - remember, it is unique, so it will only fetch back Stephen King. That's a ForeignKey - it points to a pk on a related table, and represents a Many-To-One relationship, i.e. various books can point to the same pk of one author, but not the other way around. That way each author can have many related books, but every book has only one author.

现在假设我们要添加斯蒂芬金的另一本书.这个叫做护身符:

Now let's say we want to add another book by Stephen King. This one is called The Talisman:

+----+--------------+---------+--------+
| pk |    title     |  genre  | author |
+----+--------------+---------+--------+
|  1 | Pet Semetary | Horror  |      1 |
|  2 | The Talisman | Fantasy |      1 |
+----+--------------+---------+--------+

但是,哦,等等 - 最后一个实际上是与另一位名为 Peter Straub 的作者合着的.那么我们该怎么办?我们首先需要将 Straub 添加到我们的作者表中:

But uh oh, wait - this last one was actually co-written with another author called Peter Straub. So what do we do? We need first to add Straub to our authors table:

+----+------------+-----------+-----------------------+
| pk | first_name | last_name |         email         |
+----+------------+-----------+-----------------------+
|  1 | Stephen    | King      | stephenking@gmail.com |
|  2 | J.D.       | Salinger  |                       |
|  3 | Peter      | Straub    |                       |
+----+------------+-----------+-----------------------+

但是现在我们如何告诉表格 The Talisman 与两个不同的行相关?简单 - 使用第三个表将两者联系起来.

But now how do we tell the tables that The Talisman is related to two different rows? Simple - use a third table to link the two.

所以表一是作者(如上所示).第二张桌子是书.第三个表将被称为authors_books,看起来像这样:

So table one would be authors (as seen above). second table will be books. and the third table will be called authors_books and will look like this:

+------------+--------------+
| pk_of_book | pk_of_author |
+------------+--------------+
|          1 |            1 |
|          2 |            1 |
|          2 |            3 |
+------------+--------------+

看到了吗?它告诉您如何在表之间链接不同的 pk.这是一种多对多关系,因为不同的书籍可能与不同的作者相关,反之亦然.而我描述的三表模式就是它的基本设计.

See? It tells you how to link different pks between the tables. This is a Many-To-Many relationship, because different books can be related to different authors and vice versa. And the three-table schema I described is the basic design for it.

OneToOne 关系类似于 ForeignKey,但具有 unique=True,因此您只能将一个对象链接到另一个对象,仅此而已.它通常用于在您想扩展某个模型而不更改原始模型时(比如您想将自己的自定义字段添加到内置的 User 模型中).

OneToOne relationships are like ForeignKey but with a unique=True so you can only link between one object to another object and that's it. It is usually used when you want to expand a certain model without changing the original (say you want to add your own custom field to the built in User model).

希望有助于澄清问题.Django 非常棒,您几乎不需要使用 SQL,但它仍然有助于了解一些后台发生的事情.网络上有很多关于这些关系的解释,我只给了你一个关于它的小一般介绍,我强烈建议你谷歌一下,扩大你自己的理解.祝你好运!

Hope that helps clear things up. Django is so wonderful that you almost never need to use SQL, but it still helps to know a little about what's happening in the background. There are plenty explanations about those relationship out in the web, I only gave you a small general intro about it, and I strongly suggest you google around a bit and expand your understanding for yourself. good luck!

这篇关于django之间的区别 - 一对一,多对一和多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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