简体   繁体   English

如何获得关于django关系的更直观的感受(例如:多对一,多对多)

[英]How can I get more intuitive feels about django relationships(like:Many-to-one,Many-to-many )

i use xampp(it has mysql) 我使用xampp(它有mysql)

I was Confused on this django relationships, who can give me a code example(or text) to let me feel it intuitive .thanks (like:Einstein described the theory of relativity) 我对这种django关系感到困惑,谁能给我一个代码示例(或文本),让我感觉很直观。谢谢(例如:爱因斯坦描述了相对论)

I looked all over for a simple explanation of relationships, but couldn't find anything, so I'll try to summarize it here. 我四处寻找有关关系的简单说明,但找不到任何内容,因此在这里我将对其进行总结。

Relationships aren't strictly a Django thing. 关系并不是严格意义上的Django。 If you really want to understand what Django is doing, learn about database concepts in general. 如果您真的想了解Django在做什么,请大致了解数据库概念。


When you have multiple tables of information, you need to link them somehow. 当您有多个信息表时,需要以某种方式链接它们。 If you operate a music site like last.fm, you're going to need to know about artists, genres, tags, songs, albums, etc. All of this data relates somehow. 如果您经营一个诸如last.fm之类的音乐网站,您将需要了解艺术家,流派,标签,歌曲,专辑等。所有这些数据都以某种方式相关。

For example, One artist will have many albums (one to many), one genre will apply to many artists (One to many.) eg Metallica (one artist) will have several albums, Black Album, St. Anger, etc. but one album will probably not belong to two artists, eg Alicia Keys and Metallica both recording the same album. 例如,一位艺术家将有很多专辑(一对多),一种流派将适用于许多艺术家(一对多),例如Metallica(一位艺术家)将拥有几张专辑,Black Album,St。Anger等,但是一张专辑可能不会属于两个艺术家,例如Alicia Keys和Metallica都录制同一张专辑。 To achieve this relationship, each Album record must have an artist_id to indicate which artist it is related to. 为了实现这种关系,每个专辑记录必须具有artist_id来指示与之相关的艺术家。

mysql> select * from albums where artist_id = 40;
+-----+------------------------------+------+---------------------+-----------+----------+------------+
| id  | name                         | year | created_at          | artist_id | genre_id | updated_at |
+-----+------------------------------+------+---------------------+-----------+----------+------------+
| 309 | Reise, Reise                 | 2004 | 2009-11-22 16:01:13 |        40 |        2 | NULL       | 
| 310 | Mutter                       | 2001 | 2009-11-22 16:12:28 |        40 |        2 | NULL       | 
| 311 | Sehnsucht                    | 1998 | 2009-11-22 16:20:22 |        40 |        2 | NULL       | 
| 312 | Live aus Berlin              | 1999 | 2009-11-22 16:29:11 |        40 |        2 | NULL       | 
| 313 | Rosenrot                     | 2005 | 2009-11-22 16:40:43 |        40 |        4 | NULL       | 
| 314 | The Very Best of Rammstein   |    0 | 2009-11-22 16:51:38 |        40 |        2 | NULL       | 
| 315 | Live aus Berlin (bonus disc) |    0 | 2009-11-22 17:05:24 |        40 |        2 | NULL       | 
+-----+------------------------------+------+---------------------+-----------+----------+------------+
7 rows in set (0.02 sec)

A tag will describe several artists (eg Metal describes Metallica, Pantera, and Sepultura), and one artist will have several tags (eg people might tag Metallica as Metal, Rock, and 80s Metal.) This kind of relationship between data would probably produce three tables. 标签将描述多个艺术家(例如Metal描述Metallica,Pantera和Sepultura),而一个艺术家将具有多个标签(例如人们可能将Metallica标记为Metal,Rock和80s Metal。)数据之间的这种关系很可能会产生三个表。 An artists table, a tags table, and a join table. 艺术家表,标签表和联接表。 Your join records would look like this for example (purely imaginary and hypothetical situation) 您的加入记录看起来像这样(纯属虚构和假设情况)

| id | artist_id | tag_id |
| 1  | 34        | 357    |
| 2  | 98        | 234    |

the artist_id of 34 might be Metallica, and the tag_id of 357 might be Metal. 34的artist_id可能是Metallica,357的tag_id可能是Metal。 The point is, there's a table that exists to link tags and artists. 关键是,存在一个用于链接标签和艺术家的表格。 In this example. 在这个例子中。

In general, relationships are a way to link records. 通常,关系是链接记录的一种方式。 There are three main relationships, One to One, Many to Many, and Many to One. 存在三种主要关系,一对一,多对多和多对一。

The best way to fully understand this is to learn Database Design . 充分理解这一点的最好方法是学习数据库设计

It's hard to answer a confused feeling question, but if you want code perhaps try http://www.djangosnippets.org/ 很难回答一个困惑的问题,但是如果您想编写代码,请尝试http://www.djangosnippets.org/

Also the tutorial gives great examples on how the models work in such cases as many-to-many, see http://www.djangobook.com/en/1.0/chapter05/ 此外,本教程还提供了有关模型在多对多情况下如何工作的出色示例,请参见http://www.djangobook.com/en/1.0/chapter05/

For example: 例如:

from django.db import models

class Publisher(models.Model):
    name = models.CharField(maxlength=30)
    address = models.CharField(maxlength=50)
    city = models.CharField(maxlength=60)
    state_province = models.CharField(maxlength=30)
    country = models.CharField(maxlength=50)
    website = models.URLField()

class Author(models.Model):
    salutation = models.CharField(maxlength=10)
    first_name = models.CharField(maxlength=30)
    last_name = models.CharField(maxlength=40)
    email = models.EmailField()
    headshot = models.ImageField(upload_to='/tmp')

class Book(models.Model):
    title = models.CharField(maxlength=100)
    authors = models.ManyToManyField(Author)
    publisher = models.ForeignKey(Publisher)
    publication_date = models.DateField()

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

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