django difference between - one to one, many to one and many to many

1,203 views
Skip to first unread message

Aamu Padi

unread,
Oct 28, 2013, 3:06:20 PM10/28/13
to django...@googlegroups.com

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.

N.B: I have looked at this link for that, but I still have some doubts about one-to-one and many-to-many. Suppose I want to extend with some more user information in a different class. Can I use ForeignKey instead of One-to-One field? And will anyone please be kind enough to explain to me about many-to-many relationship, (I didn't quit understood all those toppings and membership thing).

Thanks.

C. Kirby

unread,
Oct 28, 2013, 4:07:37 PM10/28/13
to django...@googlegroups.com
Those concepts are actually RDBMS (Database) ideas. You would probably be better off reading up on those. This looks to be a pretty good explanation:
http://net.tutsplus.com/tutorials/databases/sql-for-beginners-part-3-database-relationships/

Aamu Padi

unread,
Oct 29, 2013, 2:28:24 PM10/29/13
to django...@googlegroups.com
Thank you so much! I will look into that! :)

Sandeep kaur

unread,
Oct 29, 2013, 3:13:55 PM10/29/13
to django-users

On Tue, Oct 29, 2013 at 12:36 AM, Aamu Padi <aamu...@gmail.com> wrote:
>
> 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

many to one :
This relationship is used as foreign key. In this one table can be used as a parent for many children.
Eg :

class Manufacturer(models.Model):
    # ...
    pass

class Car(models.Model):
    manufacturer = models.ForeignKey('Manufacturer')
    # ...

class Bus(models.Model):
    manufacturer = models.ForeignKey('Manufacturer')
    # ...


Here there is one base table Manufacturer which acts as base for 2 other tables.

many to many :
When you want many rows of a table to be linked to many rows of another table, we use manytomany field.

class Vehicle(models.Model): 
   # ..
   manufacturer = models.ManyToManyField(Manufacturer)

Application : For making checkboxes

one to one :
This is similar to foreign key, except that one parent has only one child.
Conceptually, this is similar to a ForeignKey with unique=True, but the “reverse” side of the relation will directly return a single object.

class Car(models.Model):
    manufacturer = OneToOneField('Manufacturer')
    # ...


Hope this helped.



--
Sandeep Kaur
E-Mail: mkaur...@gmail.com
Blog: sandymadaan.wordpress.com



Aamu Padi

unread,
Oct 30, 2013, 9:45:10 AM10/30/13
to django...@googlegroups.com
Thank you! That did helped.
Reply all
Reply to author
Forward
0 new messages