Skip to content

Commit

Permalink
chore: consistently lower case the query in at search level
Browse files Browse the repository at this point in the history
At the moment, because the query is lower-cased in `def search`, there is no way to reimplement and of the `match_` methods with case sensitive search.

The query is already being lower-cased in `match_app` and `match_model`. Added lower-casing to `filter_field` and removed it from `search`.

This will amke the plugin more flexible.
  • Loading branch information
gersmann committed Apr 4, 2024
1 parent b86508b commit 3c0a08b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions admin_site_search/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def search(self, request):
"""Returns a JsonResponse containing results from matching the "q" query parameter to
application names, model names, and all instance CharFields. Only apps/models that the
user has permission to view are searched."""
query = request.GET.get("q", "").lower()
query = request.GET.get("q", "")

results = {"apps": []}
counts = {"apps": 0, "models": 0, "objects": 0}
Expand Down Expand Up @@ -154,8 +154,9 @@ def match_objects(

def filter_field(self, query: str, field: Field) -> Optional[Q]:
"""Returns a Q 'icontains' filter for Char fields, otherwise None"""
_query = query.lower()
if isinstance(field, CharField):
return Q(**{f"{field.name}__icontains": query})
return Q(**{f"{field.name}__icontains": _query})

def get_model_class(self, app_label: str, model_dict: dict) -> Optional[Model]:
"""Retrieve the model class from the dict created by admin.AdminSite, which (by default) contains:
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = django-admin-site-search
version = 0.4.0
version = 0.4.1
description = A search (cmd+k) modal, for the Django admin UI, that searches your entire site.
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down

0 comments on commit 3c0a08b

Please sign in to comment.