Replies: 2 comments 1 reply
-
The closest way I found still includes building custom expression. And uses custom list subclass hack to exclude date and time fields from filtering: class Exclude(list):
"""
A list which instance is always True, even if empty.
Tricks ninja.FilterSchema to skip pydantic field from generating queryset.
"""
def __bool__(self):
return True
class Filters(FilterSchema):
date__gt: Optional[date | Literal["null", ""]] = Field(None, q=Exclude())
time__gt: Optional[time | Literal["null", ""]] = Field(None, q=Exclude())
def __call__(self):
q = self._connect_fields()
if self.date__gt and self.time__gt:
q &= Q(timestamp__gt=datetime.combine(self.date__gt, self.time__gt))
return Mymodel.objects.filter(q) |
Beta Was this translation helpful? Give feedback.
0 replies
-
would this work ? class Filters(FilterSchema):
date__gt: date = None
time__gt: time = None
def custom_expression(self) -> Q:
q = Q()
if self.date__gt and self.time__gt:
q &= Q(timestamp__gt=datetime.combine(self.date__gt, self.time__gt))
return q |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
My frontend filtering form sends separate date and time fields. On the backend I need to validate both fields, merge them into one sql query Q(timestamp__gt=<datetime>), and exclude the separate date and time fields in sql query.
I wonder how I can achieve that without resorting to writing the whole custom_expression() query?
Beta Was this translation helpful? Give feedback.
All reactions