Skip to content

Commit 58fe85c

Browse files
authored
fix invalid lookup params (#87)
1 parent 09cedfd commit 58fe85c

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

django_typesense/changelist.py

+20-25
Original file line numberDiff line numberDiff line change
@@ -427,31 +427,26 @@ def get_queryset(self, request):
427427
if new_qs is not None:
428428
qs = new_qs
429429

430-
lookup_params_keys = remaining_lookup_params.keys()
431-
model_field_names = set((local_field.name for local_field in self.model._meta.local_fields))
432-
for key in lookup_params_keys:
433-
if key not in model_field_names:
434-
# means k only available in typesense
435-
value = remaining_lookup_params.pop(key)
436-
new_lookup_params = self.model.collection_class.get_django_lookup(key, value)
437-
remaining_lookup_params.update(new_lookup_params)
438-
439-
try:
440-
# Finally, we apply the remaining lookup parameters from the query
441-
# string (i.e. those that haven't already been processed by the
442-
# filters).
443-
qs = qs.filter(**remaining_lookup_params)
444-
except (SuspiciousOperation, ImproperlyConfigured):
445-
# Allow certain types of errors to be re-raised as-is so that the
446-
# caller can treat them in a special way.
447-
raise
448-
except Exception as e:
449-
# Every other error is caught with a naked except, because we don't
450-
# have any other way of validating lookup parameters. They might be
451-
# invalid if the keyword arguments are incorrect, or if the values
452-
# are not in the correct type, so we might get FieldError,
453-
# ValueError, ValidationError, or ?.
454-
raise IncorrectLookupParameters(e)
430+
for param, value in remaining_lookup_params.items():
431+
try:
432+
# Finally, we apply the remaining lookup parameters from the query
433+
# string (i.e. those that haven't already been processed by the
434+
# filters).
435+
qs = qs.filter(**{param: value})
436+
except (SuspiciousOperation, ImproperlyConfigured):
437+
# Allow certain types of errors to be re-raised as-is so that the
438+
# caller can treat them in a special way.
439+
raise
440+
except Exception as e:
441+
# Every other error is caught with a naked except, because we don't
442+
# have any other way of validating lookup parameters. They might be
443+
# invalid if the keyword arguments are incorrect, or if the values
444+
# are not in the correct type, so we might get FieldError,
445+
# ValueError, ValidationError, or ?.
446+
447+
# for django-typesense, possibly means k only available in typesense
448+
new_lookup_params = self.model.collection_class.get_django_lookup(param, value, e)
449+
qs = qs.filter(**new_lookup_params)
455450

456451
# Apply search results
457452
qs, search_may_have_duplicates = self.model_admin.get_search_results(

django_typesense/collections.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -198,21 +198,25 @@ def get_field(cls, name) -> TypesenseField:
198198
return fields[name]
199199

200200
@classmethod
201-
def get_django_lookup(cls, field, value) -> dict:
201+
def get_django_lookup(cls, field, value, exception: Exception) -> dict:
202202
"""
203203
Get the lookup that would have been used for this field in django. Expects to find a method on
204204
the collection called `get_FIELD_lookup` otherwise a NotImplementedError is raised
205205
206206
Args:
207-
collection_field_name: the name of the field in the collection
207+
field: the name of the field in the collection
208208
value: the value to look for
209+
exception: the django exception that led us here
209210
210211
Returns:
211212
A dictionary of the fields to the value.
212213
"""
213214

214215
if "get_%s_lookup" % field not in cls.__dict__:
215-
raise NotImplementedError
216+
raise Exception([
217+
exception,
218+
NotImplementedError("get_%s_lookup is not implemented" % field)
219+
])
216220

217221
method = methodcaller("get_%s_lookup" % field, value)
218222
return method(cls)

0 commit comments

Comments
 (0)