When I want to define my business logic, I\'m struggling finding the right way to do this, because I often both need a property AND a custom queryset to get the same info. I
To avoid any duplication, one option could be:
class PickupTimeSlotManager(models.Manager):
def get_queryset(self):
return super().get_queryset().annotate(
db_nb_bookings=Count(
'order', filter=Q(order__status=Order.VALIDATED)
)
)
from django.db import models
from .managers import PickupTimeSlotManager
class PickupTimeSlot(models.Model):
...
# Add custom manager
objects = PickupTimeSlotManager()
advantage: the calculated properties is transparently added to any queryset; no further action is required to use it
disadvantage: the computational overhead occurs even when the calculated property is not used