Custom model managers and custom querysets let Django developers add extra methods to or modify the initial queryset for a model. Using these promotes the “don’t repeat yourself” (DRY) principle in software development and promotes reuse of common queries.

import datetime

from django.db import models

class TickerQuerySet(models.QuerySet):
    def annotate_latest_price(self):
        return self.annotate(
            latest_price=TickerPrice.objects.filter(
                ticker=models.OuterRef("pk")
            )
            .order_by("-close_date")
            .values("price")[:1]
        )

    def prefetch_related_yesterday_and_today_prices(self):
        today = datetime.datetime.today()
        yesterday = today - datetime.timedelta(days=1)
        return self.prefetch_related(
            models.Prefetch(
                "ticker_prices",
                queryset=TickerPrice.objects.filter(
                    models.Q(close_date=today)
                    | models.Q(close_date=yesterday)
                ),
            )
        )

class TickerManager(models.Manager):
    def get_queryset(self):
        return TickerQuerySet(self.model, using=self._db)

class Ticker(models.Model):
    symbol = models.CharField(max_length=50, unique=True)
    objects = TickerManager()

class TickerPrice(models.Model):
    ticker = models.ForeignKey(
        Ticker, on_delete=models.CASCADE, related_name="ticker_prices"
    )
    price = models.DecimalField(max_digits=7, decimal_places=2)
    close_date = models.DateField()

In the above code, we’ve created a custom queryset with some of the previously demonstrated queries as methods. We added this new queryset to our custom manager and overrode the default objects manager on the Ticker model with our custom manager. With the custom manager and queryset, we can do the following.

tickers_with_prefetch = (
    Ticker.objects.all().prefetch_related_yesterday_and_today_prices()
)
tickers_with_latest_price = Ticker.objects.all().annotate_latest_price()

Instead of having to write the actual query for each of these examples, we call the methods defined in the custom queryset. This is especially useful if we use these queries in multiple places throughout the codebase.

Related Posts

  • Use Prefetch Objects to Control Your Prefetch Related
  • Using Q Objects for Complex Queries
  • Annotate Querysets to Fetch Specific Values
  • Optimize Database Calls with Prefetch Related and Select Related
  • Migrating your Wagtail site to a different database engine