question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

search_fields attribute / property

See original GitHub issue

Hello. I was looking trough the source code and the tutorial / documentation.

Here’s the code from documentation, when it comes to registering an autocomplete view:

class CountryAutocomplete(autocomplete.Select2QuerySetView):
    def get_queryset(self):
        # Don't forget to filter out results depending on the visitor !
        if not self.request.user.is_authenticated():
            return Country.objects.none()

        qs = Country.objects.all()

        if self.q:
            qs = qs.filter(name__istartswith=self.q)

        return qs

However, I was wondering whether we could keep simple things simple. Namely, when we register the autocomplete view, like so:

urlpatterns = [
    url(
        r'^country-autocomplete/$',
        CountryAutocomplete.as_view(),
        name='country-autocomplete',
    ),
]

could we have a simpler syntax, something like…

urlpatterns = [
    url(
        r'^country-autocomplete/$',
        name='country-autocomplete',
        model=Country,
        search_fields=('foo', 'bar__baz'),
    ),
]

then of course the underlying logic would construct a Q() object:

qs = super( something, self).get_queryset()
if self.q:
    filters = Q()
    for field in self.search_fields:
        filters |= Q(**{'%s__icontains' % field: self.q})
    qs = qs.filter(filters)

What I mean is that I totally get the point of doing a generic view when there is complex logic involved (like creating objects, complex / custom filtering, etc, etc, etc). However dal can also be used to very simple tasks and this whole block of code (extending a class) contains a single method to handle simple filtering - seems to be an overkill.

If you would agree to such changeset, I would be more than happy to provide a PR

my kindest regards,

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
mik-lajcommented, Aug 14, 2017

I guess it’s worth the look to be identical to django.admin, so I just leave the link https://github.com/django/django/blob/c19b56f633e172b3c02094cbe12d28865ee57772/django/contrib/admin/options.py#L887-L918

0reactions
toudicommented, Jul 13, 2017

Just managed to find some free time to code today, this feature is not dead yet. So terribly sorry for the delay 😦

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding search fields to existing search pages - IBM
You can add search fields such as radio buttons, text inputs, ... In this case, ensure that the request attribute defines an input...
Read more >
SearchParameters.SearchFields Property - Microsoft Learn
Gets or sets the list of field names to which to scope the full-text search. When using fielded search (fieldName:searchExpression) in a full...
Read more >
Semantic-ui search - access object properties not used in ...
I am using the Semantic UI search for the title property of my data object. data has other fields and I want to...
Read more >
Search property | Article
The attributes defined in the Search node will be displayed at runtime as a string to make text searches in the entity's fields....
Read more >
Retrieve selected fields from a search | Elasticsearch Guide [8.5]
If the includes property is specified, only source fields that match one of its patterns are returned. You can exclude fields from this...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found