本文介绍了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         |
+----+------------+-----------+-----------------------+

当您添加新值(例如斯蒂芬·金")时,它将像下面这样添加到authors表中:

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      | [email protected] |
+----+------------+-----------+-----------------------+

我们再添加一个:

+----+------------+-----------+-----------------------+
| pk | first_name | last_name |         email         |
+----+------------+-----------+-----------------------+
|  1 | Stephen    | King      | [email protected] |
|  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.

现在,我们要添加另一本斯蒂芬·金(Stephen King)的书.这个叫做护身符:

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添加到我们的authors表中:

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      | [email protected] |
|  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,因此您只能在一个对象与另一个对象之间链接,仅此而已.通常在需要扩展特定模型而不更改原始模型时使用(例如,要将自己的自定义字段添加到内置用户模型中).

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,但是仍然可以帮助您了解一些有关后台发生的事情.网络上有很多关于这些关系的解释,我只给您提供了一个小小的一般介绍,我强烈建议您在Google上稍作介绍,并自己扩展自己的理解.祝你好运!

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之间的区别-一对一,多对一和多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 09:34