[Solved]-Is it possible to override .objects on a django model?

34👍

You can do this by writing a custom Manager — just override the get_queryset method and set your objects to a Manager instance. For example:

class MyModelManager(models.Manager):
    def get_queryset(self):
        return super(MyModelManager, self).get_queryset().filter(published=True)

class MyModel(models.Model):
    # fields
    # ...

    objects = MyModelManager()

See the docs for details. It’s sensible if that’s going to be your usual, default case. To get unpublished, create another manager which you can access with something like MyModel.unpublished_objects. Again, the docs have examples on this type of thing.

👤ars

Leave a comment