Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ManyToOneRel and ForeignKey?

In django, what's the difference between a ManyToOneRel and a ForeignKey field?

like image 454
sfendell Avatar asked Mar 08 '13 18:03

sfendell


People also ask

What is the difference between ForeignKey and OneToOneField?

The only difference between these two is that ForeignKey field consists of on_delete option along with a model's class because it's used for many-to-one relationships while on the other hand, the OneToOneField, only carries out a one-to-one relationship and requires only the model's class.

WHAT IS models ForeignKey?

ForeignKey is a Django ORM field-to-column mapping for creating and working with relationships between tables in relational databases.

What is the difference between ForeignKey and many to many relationship?

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.

What is On_delete models Cascade?

The on_delete method is used to tell Django what to do with model instances that depend on the model instance you delete. (e.g. a ForeignKey relationship). The on_delete=models. CASCADE tells Django to cascade the deleting effect i.e. continue deleting the dependent models as well.


1 Answers

Django relations model exposes (and documents) only OneToOneField, ForeignKey and ManyToManyField, which corresponds to the inner

  • OneToOneField -> OneToOneRel
  • ForeignKey -> ManyToOneRel
  • ManyToManyField -> ManyToManyRel

See source of django.db.models.fields.related for further details.

like image 179
Yauhen Yakimovich Avatar answered Sep 18 '22 19:09

Yauhen Yakimovich