From c3ff7147dbf51f485b548386f06079f1ed3d9fa2 Mon Sep 17 00:00:00 2001 From: Birger Schacht Date: Tue, 1 Oct 2024 14:22:37 +0200 Subject: [PATCH] feat(generic): use the `default_search_fields` method This commit uses the recently introduced `default_search_fields` method in the ModelSearchFilter instead of implementing the same functionality there. --- apis_core/apis_entities/filtersets.py | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/apis_core/apis_entities/filtersets.py b/apis_core/apis_entities/filtersets.py index 0b5290f8f..e228b1859 100644 --- a/apis_core/apis_entities/filtersets.py +++ b/apis_core/apis_entities/filtersets.py @@ -5,13 +5,14 @@ from django.db.models import Case, Q, Value, When from django.db.models.functions import Concat from django.forms import DateInput +from django.utils.encoding import force_str from simple_history.utils import get_history_manager_for_model from apis_core.apis_entities.utils import get_entity_classes from apis_core.apis_metainfo.models import RootObject from apis_core.apis_relations.models import Property, Triple from apis_core.generic.filtersets import GenericFilterSet, GenericFilterSetForm -from apis_core.generic.helpers import generate_search_filter +from apis_core.generic.helpers import default_search_fields, generate_search_filter ABSTRACT_ENTITY_COLUMNS_EXCLUDE = [ "rootobject_ptr", @@ -106,16 +107,10 @@ def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) if model is not None and "help_text" not in self.extra: - if hasattr(model, "_default_search_fields"): - fields = model._default_search_fields - else: - modelfields = model._meta.fields - fields = [ - field.name - for field in modelfields - if isinstance(field, (models.CharField, models.TextField)) - ] - fields = ", ".join(fields) + field_names = [field.verbose_name for field in default_search_fields(model)] + # use force_str on the fields verbose names to convert + # lazy instances to string and join the results + fields = ", ".join(map(force_str, field_names)) self.extra["help_text"] = f"Search in fields: {fields}" def filter(self, qs, value):