Accessors for related objects.
When a field defines a relation between two models, each model class provides an attribute to access related instances of the other model class (unless the reverse accessor has been disabled with related_name='+').
Accessors are implemented as descriptors in order to customize access and assignment. This module defines the descriptor classes.
Forward accessors follow foreign keys. Reverse accessors trace them back. For example, with the following models:
class Parent(Model): pass class Child(Model): parent = ForeignKey(Parent, related_name='children') ``child.parent`` is a forward many-to-one relation. ``parent.children`` is a
reverse many-to-one relation.
There are three types of relations (many-to-one, one-to-one, and many-to-many) and two directions (forward and reverse) for a total of six combinations.
Related instance on the forward side of a many-to-one relation: ForwardManyToOneDescriptor.
Uniqueness of foreign key values is irrelevant to accessing the related instance, making the many-to-one and one-to-one cases identical as far as the descriptor is concerned. The constraint is checked upstream (unicity validation in forms) or downstream (unique indexes in the database).
Related instance on the forward side of a one-to-one relation: ForwardOneToOneDescriptor.
It avoids querying the database when accessing the parent link field in a multi-table inheritance scenario.
Related instance on the reverse side of a one-to-one relation: ReverseOneToOneDescriptor.
One-to-one relations are asymmetrical, despite the apparent symmetry of the name, because they're implemented in the database with a foreign key from one table to another. As a consequence ReverseOneToOneDescriptor is slightly different from ForwardManyToOneDescriptor.
Related objects manager for related instances on the reverse side of a many-to-one relation: ReverseManyToOneDescriptor.
Unlike the previous two classes, this one provides access to a collection of objects. It returns a manager rather than an instance.
Related objects manager for related instances on the forward or reverse sides of a many-to-many relation: ManyToManyDescriptor.
Many-to-many relations are symmetrical. The syntax of Django models requires declaring them on one side but that's an implementation detail. They could be declared on the other side without any change in behavior. Therefore the forward and reverse descriptors can be the same.
If you're looking for ForwardManyToManyDescriptor or ReverseManyToManyDescriptor, use ManyToManyDescriptor instead.
Class | ForeignKeyDeferredAttribute |
Undocumented |
Class | ForwardManyToOneDescriptor |
Accessor to the related object on the forward side of a many-to-one or one-to-one (via ForwardOneToOneDescriptor subclass) relation. |
Class | ForwardOneToOneDescriptor |
Accessor to the related object on the forward side of a one-to-one relation. |
Class | ManyToManyDescriptor |
Accessor to the related objects manager on the forward and reverse sides of a many-to-many relation. |
Class | ReverseManyToOneDescriptor |
Accessor to the related objects manager on the reverse side of a many-to-one relation. |
Class | ReverseOneToOneDescriptor |
Accessor to the related object on the reverse side of a one-to-one relation. |
Function | create_forward_many_to_many_manager |
Create a manager for the either side of a many-to-many relation. |
Function | create_reverse_many_to_one_manager |
Create a manager for the reverse side of a many-to-one relation. |