Code

Opened 2 years ago

Closed 2 years ago

Last modified 2 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.

Attachments (0)

Change History (4)

comment:1 Changed 2 years ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Type changed from Uncategorized to New feature

comment:2 Changed 2 years ago by aaugustin

  • Resolution set to duplicate
  • Status changed from new to closed
  • Triage Stage changed from Unreviewed to Accepted

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

comment:3 Changed 2 years ago by kunitoki@…

Yes sure, i missed that... sorry.

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

comment:4 Changed 2 years ago by aaugustin

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!

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.