﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
17669	Allow user specific queries as search_fields in django.contrib.admin.views.main.ChangeList	kunitoki@…	nobody	"I have some models in which you can define additional custom fields and attach to the models (something on the line of eav-django) and attach to the models by a generic foreign key. I would like to search on these additional fields when i type something in the search input in the admin ChangeList, but actually i have to redefine the whole ChangeList class in order to do this: i would love if we can add a user callback in this code, to allow to specify additional Q objects for search words to be put in OR, and avoid me to duplicate the whole changelist class code.

We have:
{{{
class ChangeList(object):

    def get_query_set(self):

        ...

        if self.search_fields and self.query:
            orm_lookups = [construct_search(str(search_field))
                           for search_field in self.search_fields]
            for bit in self.query.split():
                or_queries = [models.Q(**{orm_lookup: bit})
                              for orm_lookup in orm_lookups]
                qs = qs.filter(reduce(operator.or_, or_queries))

            ...
}}}

But would be practical if we do merge additional models.Q by allowing pre-search queries and precompute search fields:

{{{
class ChangeList(object):

    def get_query_set(self):

        ...

        if self.search_fields and self.query:
            orm_lookups = [construct_search(str(search_field))
                           for search_field in self.search_fields]
            for bit in self.query.split():
                or_queries = [models.Q(**{orm_lookup: bit})
                              for orm_lookup in orm_lookups]
                or_queries += self._get_additional_queries_for_bit(bit, construct_search)
                qs = qs.filter(reduce(operator.or_, or_queries))

            ...

    def _get_additional_queries_for_bit(self, bit, construct_search=lambda s: s):
        return []
}}}


For example, my own application specific implementation would be to only specify this instead of copying the whole get_query_set method:
{{{
class MyOwnChangeList(ChangeList):

    def _get_additional_queries_for_bit(self, bit, construct_search=lambda s: s):
        content_type = ContentType.objects.get_for_model(self.model)
        custom = ContentTypeCustomField.objects.filter(content_type=content_type)
        or_queries = []
        for c in custom:
            query = models.Q(**{ 'custom_field': c, 'content_type': content_type, construct_search(str('value_text')): bit })
            found = ContentTypeCustomFieldValue.objects.filter(query)
            for f in found:
                or_queries.append(models.Q(**{ str(self.model._meta.pk.name): f.object_id }))
        return or_queries    
}}}


This way a lot of useful things would be possible for searching in unrelated fields not available as search_fields. "	New feature	closed	contrib.admin	1.3-rc	Normal	duplicate	feature search_fields admin ChangeList eav-django		Accepted	0	0	0	0	0	0
