简体   繁体   中英

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

i use xampp(it has 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)

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. If you really want to understand what Django is doing, learn about database concepts in general.


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.

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. To achieve this relationship, each Album record must have an artist_id to indicate which artist it is related to.

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. 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. 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/

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/

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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