search_fields attribute / property
See original GitHub issueHello. 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:
 - Created 7 years ago
 - Comments:10 (9 by maintainers)
 

Top Related StackOverflow Question
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
Just managed to find some free time to code today, this feature is not dead yet. So terribly sorry for the delay 😦