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.
所以,这是我第一次学习计算机语言。我选择了python和django。现在,我得到了许多python和django的基本概念。我可以创建包含视图和所有其他内容的新页面。但我仍然对这种关系感到困惑,即一对一,多对一,多对多。请有人请你解释一下。我怎么用呢?我真的需要知道。
Thanks.
2 个解决方案
#1
51
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.
特别是django,它使复杂的数据库设计变得轻而易举,我认为了解它们如何在SQL级别上工作以更好地理解您正在做的事情非常重要。我认为一个例子是了解这一点的最佳方式。
First thing you should understand is that each SQL table has one field (which is usually automatically incremented) which is called a 'prime-key'. This field is a column with a unique value for each row.
您应该了解的第一件事是每个SQL表都有一个字段(通常会自动递增),称为“主键”。此字段是一列,每行具有唯一值。
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 prime-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:
在django中,您可以创建一个表示作者的模型,该模型有三个字段 - first_name,last_name和包含电子邮件的可选字段。 Django还会自动添加主键字段并将其命名为pk(您也可以决定定义自己的字段以用作主键但通常不会)。因此,当使用命令manage.py syncdb时,它将创建一个如下所示的表:
+----+------------+-----------+-----------------------+
| pk | first_name | last_name | email |
+----+------------+-----------+-----------------------+
When you add a new value (say 'Stephen King') it would add it to the authors table like so:
当你添加一个新值(比如'Stephen King')时,它会将它添加到authors表中,如下所示:
+----+------------+-----------+-----------------------+
| pk | first_name | last_name | email |
+----+------------+-----------+-----------------------+
| 1 | Stephen | King | stephenking@gmail.com |
+----+------------+-----------+-----------------------+
Let's add another one:
让我们再添一个:
+----+------------+-----------+-----------------------+
| pk | first_name | last_name | email |
+----+------------+-----------+-----------------------+
| 1 | Stephen | King | stephenking@gmail.com |
| 2 | J.D. | Salinger | |
+----+------------+-----------+-----------------------+
That's simple. Now we add a new model called Book:
这很简单。现在我们添加一个名为Book的新模型:
+----+--------------+--------+--------+
| pk | title | genre | author |
+----+--------------+--------+--------+
| 1 | Pet Semetary | Horror | 1 |
+----+--------------+--------+--------+
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.
现在看看我在那里做了什么?在作者的领域,我给了斯蒂芬金的主要钥匙的价值 - 记住,它是独一无二的,所以它只会取回斯蒂芬金。这是一个ForeignKey - 它指向相关表上的pk,并表示多对一关系,即各种书籍可以指向一个作者的相同pk,但不是相反。这样每个作者都可以拥有许多相关书籍,但每本书只有一位作者。
Now let's say we want to add another book by Stephen King. This one is called The Talisman:
现在让我们说我们想要添加斯蒂芬金的另一本书。这个被称为The Talisman:
+----+--------------+---------+--------+
| pk | title | genre | author |
+----+--------------+---------+--------+
| 1 | Pet Semetary | Horror | 1 |
| 2 | The Talisman | Fantasy | 1 |
+----+--------------+---------+--------+
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:
但是,哦,等等 - 这最后一个实际上是与另一位名叫Peter Straub的作者共同编写的。那么我们该怎么办?我们首先需要将Straub添加到authors表中:
+----+------------+-----------+-----------------------+
| pk | first_name | last_name | email |
+----+------------+-----------+-----------------------+
| 1 | Stephen | King | stephenking@gmail.com |
| 2 | J.D. | Salinger | |
| 3 | Peter | Straub | |
+----+------------+-----------+-----------------------+
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.
但是现在我们如何告诉表格The Talisman与两个不同的行有关?简单 - 使用第三个表来链接这两个。
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:
所以表一是作者(如上所示)。第二张桌子就是书。第三个表将被称为authors_books,如下所示:
+------------+--------------+
| pk_of_book | pk_of_author |
+------------+--------------+
| 1 | 1 |
| 2 | 1 |
| 2 | 3 |
+------------+--------------+
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.
看到?它告诉您如何链接表之间的不同pks。这是一种多对多关系,因为不同的书籍可能与不同的作者有关,反之亦然。我描述的三表模式是它的基本设计。
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).
OneToOne关系类似于ForeignKey但具有唯一= True,因此您只能将一个对象链接到另一个对象,就是这样。当您想要在不更改原始模型的情况下扩展某个模型时(通常是您要将自己的自定义字段添加到内置的用户模型中),通常会使用它。
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非常精彩,几乎不需要使用SQL,但它仍然有助于了解后台发生的事情。关于这些关系在网上有很多解释,我只给你一个关于它的小概述介绍,我强烈建议你稍微谷歌并扩大你对自己的理解。祝你好运!
#2
0
The relations are a general concept on relational databases like MySQL. Everything regarding to relations and Django´s ORM is very well explained here: https://docs.djangoproject.com/en/dev/topics/db/models/#relationships.
关系是像MySQL这样的关系数据库的一般概念。关于关系和Django的ORM的一切都在这里得到了很好的解释:https://docs.djangoproject.com/en/dev/topics/db/models/#relationships。
Hope this leads into the right direction.
希望这会导致正确的方向。
#1
51
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.
特别是django,它使复杂的数据库设计变得轻而易举,我认为了解它们如何在SQL级别上工作以更好地理解您正在做的事情非常重要。我认为一个例子是了解这一点的最佳方式。
First thing you should understand is that each SQL table has one field (which is usually automatically incremented) which is called a 'prime-key'. This field is a column with a unique value for each row.
您应该了解的第一件事是每个SQL表都有一个字段(通常会自动递增),称为“主键”。此字段是一列,每行具有唯一值。
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 prime-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:
在django中,您可以创建一个表示作者的模型,该模型有三个字段 - first_name,last_name和包含电子邮件的可选字段。 Django还会自动添加主键字段并将其命名为pk(您也可以决定定义自己的字段以用作主键但通常不会)。因此,当使用命令manage.py syncdb时,它将创建一个如下所示的表:
+----+------------+-----------+-----------------------+
| pk | first_name | last_name | email |
+----+------------+-----------+-----------------------+
When you add a new value (say 'Stephen King') it would add it to the authors table like so:
当你添加一个新值(比如'Stephen King')时,它会将它添加到authors表中,如下所示:
+----+------------+-----------+-----------------------+
| pk | first_name | last_name | email |
+----+------------+-----------+-----------------------+
| 1 | Stephen | King | stephenking@gmail.com |
+----+------------+-----------+-----------------------+
Let's add another one:
让我们再添一个:
+----+------------+-----------+-----------------------+
| pk | first_name | last_name | email |
+----+------------+-----------+-----------------------+
| 1 | Stephen | King | stephenking@gmail.com |
| 2 | J.D. | Salinger | |
+----+------------+-----------+-----------------------+
That's simple. Now we add a new model called Book:
这很简单。现在我们添加一个名为Book的新模型:
+----+--------------+--------+--------+
| pk | title | genre | author |
+----+--------------+--------+--------+
| 1 | Pet Semetary | Horror | 1 |
+----+--------------+--------+--------+
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.
现在看看我在那里做了什么?在作者的领域,我给了斯蒂芬金的主要钥匙的价值 - 记住,它是独一无二的,所以它只会取回斯蒂芬金。这是一个ForeignKey - 它指向相关表上的pk,并表示多对一关系,即各种书籍可以指向一个作者的相同pk,但不是相反。这样每个作者都可以拥有许多相关书籍,但每本书只有一位作者。
Now let's say we want to add another book by Stephen King. This one is called The Talisman:
现在让我们说我们想要添加斯蒂芬金的另一本书。这个被称为The Talisman:
+----+--------------+---------+--------+
| pk | title | genre | author |
+----+--------------+---------+--------+
| 1 | Pet Semetary | Horror | 1 |
| 2 | The Talisman | Fantasy | 1 |
+----+--------------+---------+--------+
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:
但是,哦,等等 - 这最后一个实际上是与另一位名叫Peter Straub的作者共同编写的。那么我们该怎么办?我们首先需要将Straub添加到authors表中:
+----+------------+-----------+-----------------------+
| pk | first_name | last_name | email |
+----+------------+-----------+-----------------------+
| 1 | Stephen | King | stephenking@gmail.com |
| 2 | J.D. | Salinger | |
| 3 | Peter | Straub | |
+----+------------+-----------+-----------------------+
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.
但是现在我们如何告诉表格The Talisman与两个不同的行有关?简单 - 使用第三个表来链接这两个。
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:
所以表一是作者(如上所示)。第二张桌子就是书。第三个表将被称为authors_books,如下所示:
+------------+--------------+
| pk_of_book | pk_of_author |
+------------+--------------+
| 1 | 1 |
| 2 | 1 |
| 2 | 3 |
+------------+--------------+
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.
看到?它告诉您如何链接表之间的不同pks。这是一种多对多关系,因为不同的书籍可能与不同的作者有关,反之亦然。我描述的三表模式是它的基本设计。
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).
OneToOne关系类似于ForeignKey但具有唯一= True,因此您只能将一个对象链接到另一个对象,就是这样。当您想要在不更改原始模型的情况下扩展某个模型时(通常是您要将自己的自定义字段添加到内置的用户模型中),通常会使用它。
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非常精彩,几乎不需要使用SQL,但它仍然有助于了解后台发生的事情。关于这些关系在网上有很多解释,我只给你一个关于它的小概述介绍,我强烈建议你稍微谷歌并扩大你对自己的理解。祝你好运!
#2
0
The relations are a general concept on relational databases like MySQL. Everything regarding to relations and Django´s ORM is very well explained here: https://docs.djangoproject.com/en/dev/topics/db/models/#relationships.
关系是像MySQL这样的关系数据库的一般概念。关于关系和Django的ORM的一切都在这里得到了很好的解释:https://docs.djangoproject.com/en/dev/topics/db/models/#relationships。
Hope this leads into the right direction.
希望这会导致正确的方向。