Skip to content

Commit

Permalink
Fix andreynovikov#7 : Add possibility to filter on nested field more …
Browse files Browse the repository at this point in the history
…than 1 level
  • Loading branch information
jeromelefeuvre committed Mar 29, 2023
1 parent 828a40d commit 36169a5
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions daterangefilter/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ def __init__(self, field, request, params, model, model_admin, field_path):
if self.lookup_gte and self.lookup_lte:
self.lookup_val = '{} - {}'.format(self.lookup_gte, self.lookup_lte)
# if we are filtering DateTimeField we should add one day to final date
if "__" in field_path:
related_model, field = field_path.split("__")
field = model._meta.get_field(related_model).related_model._meta.get_field(field)
else:
field = model._meta.get_field(field_path)
new_field_path = field_path
nested_model = model
while len(new_field_path.split('__')) > 1:
related_model, new_field_path = new_field_path.split("__")[0], '__'.join(new_field_path.split("__")[1:])
nested_model = nested_model._meta.get_field(related_model).related_model

field = nested_model._meta.get_field(new_field_path)

if isinstance(field, models.DateTimeField) or isinstance(field, models.DateField):
try:
Expand Down

0 comments on commit 36169a5

Please sign in to comment.