Skip to content

Commit

Permalink
Merge pull request #1 from jeromelefeuvre/filter_nested_model
Browse files Browse the repository at this point in the history
Fix andreynovikov#7 : Add possibility to filter on nested field more than 1 level
  • Loading branch information
jeromelefeuvre authored Dec 17, 2021
2 parents b5275e5 + 6913dc2 commit 490601c
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions daterangefilter/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ 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):
try:
gte_date = datetime.datetime.strptime(self.lookup_gte, '%Y-%m-%d')
Expand Down

0 comments on commit 490601c

Please sign in to comment.