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.
Thanks.
The difference between One-to-many , Many-to-one and Many-to-Many is: One-to-many vs Many-to-one is a matter of perspective. Unidirectional vs Bidirectional will not affect the mapping but will make difference on how you can access your data. In Many-to-one the many side will keep reference of the one side.
Multiple records in a table are associated with multiple records in another table. - Many car models can run on same fuel type. - Car model can run on diffrent fuel types.
The big difference in terms of table structure between one-to-one and one-to-many is that in one-to-one it is possible (but not necessary) to have a bidirectional relationship, meaning table A can have a foreign key into table B, and table B can have a foreign key into the associated record in table A.
A foreign key relationship is generally known as a many-to-one relationship. Note that the reverse of this relationship is one-to-many (which Django provides tools to access). As the name implies, many objects may be related to one.
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.
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.
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 |
+----+------------+-----------+-----------------------+
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] |
+----+------------+-----------+-----------------------+
Let's add another one:
+----+------------+-----------+-----------------------+
| pk | first_name | last_name | email |
+----+------------+-----------+-----------------------+
| 1 | Stephen | King | [email protected] |
| 2 | J.D. | Salinger | |
+----+------------+-----------+-----------------------+
That's simple. Now we add a new model called 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.
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 |
+----+--------------+---------+--------+
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 | |
+----+------------+-----------+-----------------------+
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.
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 |
+------------+--------------+
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 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).
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!
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.
Hope this leads into the right direction.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With