Working with model field relationships
Django provides three relationship types for linking tables:
- Many–to–one
- Many–to–many
- One–to–one
A many-to-one relationship is defined by using a ForeignKey
field, and the other two relationship types are defined using the self-explanatory ManyToManyField
and OneToOneField
. These fields are named appropriately after the relationship type that they represent.
Next, we will discuss the key components of working with model field relationships.
Field arguments
The three field types, ForeignKey
, ManyToManyField
, and OneToOneField
, all accept the standard default
, blank
, and verbose_name
field arguments that other field types accept. The null
argument will have no effect on a ManyToManyField
and will only apply to the ForeignKey
and OneToOneField
types. Two of these field types—ForeignKey
and OneToOneField
—require at least two positional arguments, the first being...