Skip to content

Commit

Permalink
feat(generic): allow to set default search fields in the model
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Jun 26, 2024
1 parent a156cb4 commit 763a866
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions apis_core/generic/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def generate_search_filter(model, query, fields_to_search=None, prefix=""):
"""
Generate a default search filter that searches for the `query`
in all the CharFields and TextFields of a model (case-insensitive)
or in the fields listed in the `fields_to_search` argument.
or in the fields listed in the models `default_search_fields` attribute
or in the fields listed in the `fields_to_search` argument
This helper can be used by autocomplete querysets if nothing
fancier is needed.
If the `prefix` is set, the field names will be prefixed with that string -
Expand All @@ -21,24 +22,28 @@ def generate_search_filter(model, query, fields_to_search=None, prefix=""):
"""
query = query.split()

modelfields = model._meta.fields
# search all char and text fields by default
_fields_to_search = [
field.name for field in modelfields if isinstance(field, (CharField, TextField))
]

# check if the model has a `_default_search_fields`
# list and use that as searchfields
if isinstance(getattr(model, "_default_search_fields", None), list):
_fields_to_search = model._default_search_fields

# if the method was passed a `fields_to_search` list, use that
if isinstance(fields_to_search, list):
fields_to_search = [
field.name for field in model._meta.fields if field.name in fields_to_search
]
else:
fields_to_search = [
field.name
for field in model._meta.fields
if isinstance(field, (CharField, TextField))
]
_fields_to_search = fields_to_search

q = Q()

for token in query:
q &= functools.reduce(
lambda acc, field_name: acc
| Q(**{f"{prefix}{field_name}__icontains": token}),
fields_to_search,
_fields_to_search,
Q(),
)
return q
Expand Down

0 comments on commit 763a866

Please sign in to comment.