[Fixed]-Add a choicefield to search_fields in django admin

1👍

You should be able to do this by overriding the get_search_results method of your model admin.

# This dictionary lets you convert the text back to the integer.
# You might prefer to build it dynamically instead.

TEXT_TO_STATUS_INTEGER = {
    'Started': 1,
    'DONE': 0,
}

class MyModelAdmin(admin.ModelAdmin):
    def get_search_results(self, request, queryset, search_term):
        queryset, use_distinct = super(PersonAdmin, self).get_search_results(request, queryset, search_term)
        if search_term in TEXT_TO_STATUS_INTEGER:
            queryset |= self.model.objects.filter(status=TEXT_TO_STATUS_INTEGER[search_term])
    return queryset, use_distinct

Leave a comment