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
http://pythonclub.com.br/
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...
--
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 [email protected].
To post to this group, send email to [email protected].
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.