Fluent interface on custom methods of custom Queryset class

23 views
Skip to first unread message

Fabio C. Barrionuevo da Luz

unread,
Jul 19, 2017, 2:52:51 PM7/19/17
to django...@googlegroups.com
What is the right way to make Fluent interface on custom methods of custom Queryset class?

In other words, how to make "by_validity" work without lost previous filters?

Procedure.objects.filter(is_active=False).by_validity()


I've tried using q = self or q = self.query before apply Q(start_date__lte=start_date) & (Q(end_date=None) | Q(end_date__gte=end_date)) , But it did not work, I got errors.

Thanks.

sample code:

from datetime import date, datetime, time
from django.conf import settings
from django.db import models
from django.db.models import Q


class ProcedureQuerySet(models.QuerySet):
    def by_validity(self, start_date=None, end_date=None):
        if not start_date:
            start_date = date.today()
        if start_date and not end_date:
            end_date = start_date
        if end_date < start_date:
            raise ValueError("end_date must be greater than start_date")

        start_date = datetime.combine(start_date, time.min)
        end_date = datetime.combine(end_date, time.max)


        q = Q(start_date__lte=start_date) & (Q(end_date=None) | Q(end_date__gte=end_date))
        return self.filter(q)

class ProcedureManager(models.Manager.from_queryset(ProcedureQuerySet)):
    pass


class Procedure(models.Model):
    start_date = models.DateTimeField()
    end_date = models.DateTimeField(null=True)
    is_active = models.BooleanField(default=True)

    objects = ProcedureManager()



--
Fábio C. Barrionuevo da Luz
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

Felippe Raposo

unread,
Jul 19, 2017, 3:58:58 PM7/19/17
to django...@googlegroups.com
Hey Fabio, according to the Django documentation the right way would be:

ProcedureManager = models.Manager.from_queryset(ProcedureQuerySet)

class Procedure(models.Model):
    start_date = models.DateTimeField()
    end_date = models.DateTimeField(null=True)
    is_active = models.BooleanField(default=True)

    objects = ProcedureManager()

or

class Procedure(models.Model):
    start_date = models.DateTimeField()
    end_date = models.DateTimeField(null=True)
    is_active = models.BooleanField(default=True)

    objects = models.Manager.from_queryset(ProcedureQuerySet)()


Warmly,
Felippe Raposo


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAPVjvMZ8vm_61M_xZ2KU4vjca4ke3d52MfgEX0LrcS0gaq3tKQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages