Related objects
When you define a relationship in a model (that is, a ForeignKey
, OneToOneField
, or ManyToManyField
), instances of that model will have a convenient API to access the related object(s).
Using the models at the top of this page, for example, an Entry
object e
can get its associated Blog
object by accessing the blog
attribute: e.blog
.
(Behind the scenes, this functionality is implemented by Python descriptors. This shouldn't really matter to you, but I point it out here for the curious.)
Django also creates API accessors for the other side of the relationship-the link from the related model to the model that defines the relationship. For example, a Blog
object b
has access to a list of all related Entry
objects via the entry_set
attribute: ...
Get Mastering Django: Core now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.