flake8-django icon indicating copy to clipboard operation
flake8-django copied to clipboard

DJ012: False positive when declaring multiple managers

Open YPCrumble opened this issue 2 years ago • 0 comments

Thank you for maintaining this library! I think I have found a bug in DJ12. Let's say I'm declaring multiple managers like in the docs:

# First, define the Manager subclass.
class DahlBookManager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(author="Roald Dahl")


# Then hook it into the Book model explicitly.
class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=50)

    objects = models.Manager()  # The default manager.
    dahl_objects = DahlBookManager()  # The Dahl-specific manager.

flake8-django will flag dahl_objects, I believe because DJ12.is_manager_declaration assumes that managers are only named objects.

The issue I believe is that Django's docs state that the order of managers is important, i.e., the first manager defined is always the default manager: https://docs.djangoproject.com/en/5.0/topics/db/managers/#default-managers

YPCrumble avatar Dec 20 '23 14:12 YPCrumble