[Fixed]-Django: where to put recurring query on list of model objects

1👍

I guess the ‘django way’ would be to define it as a custom manager method, instead of class method, which can be shared between different models with composition instead of inheritance in case of class methods.

from django.db import models

class ProductQuerySet(models.query.QuerySet):

      def get_uncompleted_products(self):
          ...

class ProductManager(models.Manager):

      def get_queryset(self):
          return ProductQueryset(self.model, using=self._db)

      def get_uncompleted_products(self):
          # defined twice to resolve queryset chaining issue with custom managers
          return self.get_queryset().get_uncompleted_products()

class Product(models.Model):

    ...
      objects = ProductManager()

Leave a comment