Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions docs/advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,34 @@ About Queryset Methods
* :meth:`~django.db.models.query.QuerySet.distinct` works as expected. It only regards the fields
of the base class, but this should never make a difference.

* :meth:`~django.db.models.query.QuerySet.select_related` works just as usual, but it can not
(yet) be used to select relations in inherited models (like
``ModelA.objects.select_related('ModelC___fieldxy')`` )
* :meth:`~django.db.models.query.QuerySet.select_related` works just as usual with the
exception that the query set must be derived from a PolymorphicRelatedQuerySetMixin
or PolymorphicRelatedQuerySet.

This can be achieved by using a custom manager

class NonPolyModel(models.Model):
relation = models.ForeignKey(BasePolyModel, on_delete=models.CASCADE)
objects = models.Manager.from_queryset(PolymorphicRelatedQuerySet)()

or by converting a models queryset using

class NonPolyModel(models.Model):
relation = models.ForeignKey(BasePolyModel, on_delete=models.CASCADE)
objects = models.Manager.from_queryset(QuerySet)()

``convert_to_polymorphic_queryset(NonPolyModel.objects).filter(...)``

To select related fields the model name comes after the field name and set the
field.
``ModelA.objects.filter(....).select_related('field___TargetModel__subfield')``.
or using the polymorphic added related fieldname which is normally the lowercase
version of the model name.
``ModelA.objects.filter(....).select_related('field__targetmodel__subfield')``

This automatically manages the via models between the model specified in the related
field and the target model.
``ModelA.objects.filter(....).select_related('field__targetparentmodel__targetmodel__subfield')``

* :meth:`~django.db.models.query.QuerySet.extra` works as expected (it returns polymorphic
results) but currently has one restriction: The resulting objects are required to have a unique
Expand Down
Loading