Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use as default manager? #79

Open
autoantwort opened this issue Oct 1, 2023 · 1 comment · May be fixed by #80
Open

How to use as default manager? #79

autoantwort opened this issue Oct 1, 2023 · 1 comment · May be fixed by #80

Comments

@autoantwort
Copy link

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()
            )
        )

does not seems to use CTEs

@autoantwort
Copy link
Author

Hm If I replace
https://github.com/dimagi/django-cte/blob/5c8c1e51faae228fc08ed847a2792347d070a518/django_cte/cte.py#L107C12-L107C12
by
qs = CTEQuerySet(cte_query.model)
it works

@autoantwort autoantwort linked a pull request Oct 1, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant