Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#17669 closed New feature (duplicate)

Allow user specific queries as search_fields in django.contrib.admin.views.main.ChangeList

Reported by: kunitoki@… Owned by: nobody
Component: contrib.admin Version: 1.3-rc
Severity: Normal Keywords: feature search_fields admin ChangeList eav-django
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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.

Change History (4)

comment:1 by Aymeric Augustin, 13 years ago

Type: UncategorizedNew feature

comment:2 by Aymeric Augustin, 13 years ago

Resolution: duplicate
Status: newclosed
Triage Stage: UnreviewedAccepted

Unless I missed something, this feature was already requested in #15961.

comment:3 by kunitoki@…, 13 years ago

Yes sure, i missed that... sorry.

Btw... any plans to include something more pluggable in future versions ?

comment:4 by Aymeric Augustin, 13 years ago

Like all feature requests, it will only happen if someone:

  • sends a good API proposal to django-developers
  • develops a patch (with tests and docs)
  • improves it until it's ready for checkin

There's no activity on #15961. That means that until now, nobody was sufficiently interested to take ownership of the ticket. If you want to do it, you're very welcome!

Note: See TracTickets for help on using tickets.
Back to Top