Skip to content

Commit f115219

Browse files
committed
doc: add relect_related basic docs
1 parent 631de9e commit f115219

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

docs/advanced.rst

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,27 @@ About Queryset Methods
167167
* ``distinct()`` works as expected. It only regards the fields of
168168
the base class, but this should never make a difference.
169169

170-
* ``select_related()`` works just as usual, but it can not (yet) be used
171-
to select relations in inherited models
172-
(like ``ModelA.objects.select_related('ModelC___fieldxy')`` )
170+
* ``select_related()`` works just as usual with the exception that the
171+
query set must be derived from a PolymorphicRelatedQuerySetMixin
172+
or PolymorphicRelatedQuerySet.
173+
174+
This can be achieved by using a custom manager
175+
176+
class NonPolyModel(models.Model):
177+
relation = models.ForeignKey(BasePolyModel, on_delete=models.CASCADE)
178+
objects = models.Manager.from_queryset(PolymorphicRelatedQuerySet)()
179+
180+
181+
To select related fields the model name comes after the field name and set the
182+
field.
183+
``ModelA.objects.filter(....).select_related('field___TargetModel__subfield')``.
184+
or using the polymorphic added related fieldname which is normally the lowercase
185+
version of the model name.
186+
``ModelA.objects.filter(....).select_related('field__targetmodel__subfield')``
187+
188+
This automatically manages the via models between the model specified in the related
189+
field and the target model.
190+
``ModelA.objects.filter(....).select_related('field__targetparentmodel__targetmodel__subfield')``
173191

174192
* ``extra()`` works as expected (it returns polymorphic results) but
175193
currently has one restriction: The resulting objects are required to have

0 commit comments

Comments
 (0)