Django Model Field Type: ForeignKey

By: Dusty Arlia
Published on Wednesday, March 19, 2014, 07:31 PM
Last Updated on Thursday, July 09, 2015 at 11:07 PM
Total Updates: 3

A ForeignKey allows for a many-to-one relationship with another model. This field requires the class name of the related model to be the first argument. Here is its syntax:

models.ForeignKey(Other_Model[, optional_options])

It is recommended that you use the name of the model in lowercase as the name of your ForeignKey (though not required). For example:

other_model = models.ForeignKey(Other_Model)

The ForeignKey field also accepts a number of optional arguments. These optional arguments help define the relationship.

You can also define a recursive relationship (an object that has a many-to-one relationship with itself) like this: