32👍
Since django 1.6, you can customize the search by defining a get_search_results
method in your ModelAdmin
subclass.
It is well explained in django documentation. The following example is copied from this doc.
class PersonAdmin(admin.ModelAdmin):
list_display = ('name', 'age')
search_fields = ('name',)
def get_search_results(self, request, queryset, search_term):
queryset, use_distinct = super(PersonAdmin, self).get_search_results(request, queryset, search_term)
try:
search_term_as_int = int(search_term)
queryset |= self.model.objects.filter(age=search_term_as_int)
except:
pass
return queryset, use_distinct
👤luc
-1👍
You should use search_fields = ['foreign_key__related_fieldname']
like search_fields = ['user__email']
in the admin class that extends admin.ModelAdmin
read more here
- [Django]-Django QuerySet order
- [Django]-Django Python rest framework, No 'Access-Control-Allow-Origin' header is present on the requested resource in chrome, works in firefox
- [Django]-What is actually assertEquals in Python?
-2👍
this might can help
search_fields = [‘foreign_key__related_fieldname’]
- [Django]-Using south to refactor a Django model with inheritance
- [Django]-Override save on Django InlineModelAdmin
- [Django]-Django: Create fixtures without specifying a primary key?
Source:stackexchange.com