dominno / django-moderation

django-moderation is reusable application for Django framework, that allows to moderate any model objects.
BSD 3-Clause "New" or "Revised" License
269 stars 90 forks source link

overtaking `_default_manager` #199

Open grucha opened 3 years ago

grucha commented 3 years ago

I have a use case where I don't want the mdoel._default_manager to be moderated.

I am limiting moderation-affected querysets by Moderator.manager_names and I am using moderated managers explicitly in my code only where I need them.

Unfortunately, regrdless of limiting via manager_names, django-moderation overtakes the model._default_manager and I can not create objects with foreign key to moderated model for objects that haven't been approved yet (having visible=False).

Here is a sketch of my models:

class Artwork(ModeratedModel, models.Model):
    objects = models.Manager()
    moderated_objects = models.Manager()  # only this one should be moderated

    class Meta:
        manager_names = ["moderated_objects"]  # I don't want `objects` to be affected, i.e. the default manager too!
        visibility_column = "visible"

class Photo(models.Model):
    artwork = ForeignKey(Artwork)

And the usage:

>>> artwork = Artwork.objects.create()  # has visible = False, is excluded from moderated queryset
>>> photo = Photo.objects.create(artwork=artwork)
Traceback (most recent call last):
  File "...."
ArtworkDoesNotExist: ...

This happens because django validates Photo.artist field with Artist's _default_manager: https://docs.djangoproject.com/en/3.2/topics/db/managers/#django.db.models.Model._default_manager which is moderated, thus fails (until artist was approved).