jazzband/django-model-utils

QueryManager constraints don't apply to RelatedManagers

Open

#31 geöffnet am 15. März 2013

Auf GitHub ansehen
 (7 Kommentare) (0 Reaktionen) (0 zugewiesene Personen)Python (370 Forks)github user discovery
bughelp wanted

Repository-Metriken

Stars
 (2.762 Stars)
PR-Merge-Metriken
 (PR-Metriken ausstehend)

Beschreibung

What?

Let's say you have a model like this:

class Article(models.Model):
    author = models.ForeignKey('Author')
    published = models.BooleanField()

    published_only = QueryManager(published=True)

Note that published_only is the default manager here.

Now if you do

an_author.article_set...

the published=True constraint is NOT added to the query.

Why?

When you access a related model that way, Django internally subclasses the model's default manager (db/models/fields/related.py, ForeignRelatedObjectsDescriptor:related_manager_cls) and uses that manager for the query. The issue here is that by the current implementation, django-model-utils' QueryManager keeps the constraints as instance state and not as class state. Which obviously means that the constraints are lost in the Django-generated related manager.

Solution 1

Fix this in Django: The Django-generated manager class should not only extend the default manager's class but the concrete instance. By not using inheritance for example.

Solution 2

Make QueryManager(...) call actually generate a subclass that incorporates the constraints (arguments). Probably easier but doesn't feel "right".

Contributor Guide