I tried to use a CTE as default manager, but I failed to do so:
class PeriodsManager(models.Manager):
def get_queryset(self):
# get the end date of the period, oldest period first, newest last
cte = With(
QuerySet(MembershipTypePeriods)
# MembershipTypePeriods.objects
.order_by('start_date').annotate(
end_date=Window(
partition_by=[F('membership_id')],
expression=Lead('start_date'),
order_by=F('start_date').asc()
)
))
return cte.queryset().with_cte(cte)
class MembershipTypePeriods(models.Model):
objects = PeriodsManager()
start_date = models.DateField(default=date.today)
type = models.ForeignKey(MembershipType, on_delete=RESTRICT)
membership = models.ForeignKey(Membership, on_delete=CASCADE)
Something like
class PeriodsManager(CTEManager):
def get_queryset(self):
# get the end date of the period, oldest period first, newest last
return super().get_queryset().order_by('start_date').annotate(
end_date=Window(
partition_by=[F('membership_id')],
expression=Lead('start_date'),
order_by=F('start_date').asc()
)
)
I tried to use a CTE as default manager, but I failed to do so:
Something like
does not seems to use CTEs