Add compatibility with older django/python versions

This commit is contained in:
sevdog 2023-06-22 14:41:48 +02:00
parent ad96159234
commit 82c42dc5db
No known key found for this signature in database
GPG Key ID: D939AF7A93A9C178
2 changed files with 4 additions and 1 deletions

View File

@ -96,6 +96,10 @@ class SearchFilter(BaseFilterBackend):
if hasattr(field, "path_infos"): if hasattr(field, "path_infos"):
# Update opts to follow the relation. # Update opts to follow the relation.
opts = field.path_infos[-1].to_opts opts = field.path_infos[-1].to_opts
# django < 4.1
elif hasattr(field, 'get_path_info'):
# Update opts to follow the relation.
opts = field.get_path_info()[-1].to_opts
# Otherwise, use the field with icontains. # Otherwise, use the field with icontains.
lookup = 'icontains' lookup = 'icontains'
return LOOKUP_SEP.join([field_name, lookup]) return LOOKUP_SEP.join([field_name, lookup])

View File

@ -182,7 +182,6 @@ class SearchFilterTests(TestCase):
request = factory.get('/', {'search': r'^\w{3}$', 'title_only': 'true'}) request = factory.get('/', {'search': r'^\w{3}$', 'title_only': 'true'})
response = view(request) response = view(request)
print(response.data)
assert response.data == [ assert response.data == [
{'id': 3, 'title': 'zzz', 'text': 'cde'} {'id': 3, 'title': 'zzz', 'text': 'cde'}
] ]