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
Felippe Raposo
2017-07-19 11:50 GMT-03:00 Fabio C. Barrionuevo da Luz <bnafta@gmail.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, timefrom django.conf import settingsfrom django.db import modelsfrom django.db.models import Qclass 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_dateif 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)): passclass 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 SulBlog 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...
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-users@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_ xZ2KU4vjca4ke3d52MfgEX0LrcS0ga q3tKQ%40mail.gmail.com
For more options, visit https://groups.google.com/d/optout .
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-users@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/CAP6GVqiti5VmG2X3%2Bpo3aRBhbw1dUN1kQZQ8Nk%3DTn4f%2B6NWOeA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
No comments:
Post a Comment