#10961: Allow users to override forward and reverse relationships on proxy
models
with !ForeignKey fields.
-------------------------------------+-------------------------------------
Reporter: Tai Lee | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: proxy ForeignKey | Triage Stage: Accepted
reverse relationship manager |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by powderflask):
I've run up against this issue on several projects, in fact nearly every
time I use proxy models to solve a problem. Different workarounds each
time, depending on use-case.
Currently working with M2M relations on proxy models, same issue can
occur.
This is solved nicely with prefetch_related using a Prefetch object, and
specifying the queryset from the related proxy model.
{{{
prefetch=models.Prefetch('related_set',
queryset=RelatedProxyModel.objects.all())
obj = MyModel.objects.prefetch_related(prefetch).first()
assert all( [type(r) == RelatedProxyModel for r in obj.related_set.all()]
)
}}}
The same approach should work for accessing a reverse relation.
This can all be hidden in a model Manager or Queryset to make code
prettier.
Got me to wonder if a similar approach could be used by allowing a
queryset to be specified on select_related? In fact, only need the model
class, since a queryset for select_related would over-specify.
Not as elegant as a declaration on the model, but seems like it might be
pretty flexible and if it's easy to implement, a quick-and-dirty work-
around that avoids any hackery.
--
Ticket URL: <https://code.djangoproject.com/ticket/10961#comment:13>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.ae5fdcb74f1657aed7d2daf89f330f45%40djangoproject.com.