I would like to display the function name in search fields in Django Admin interface but when i do it
The models.py:
class Adult(models.Model):
user = models.OneToOneField(User)
firstname = models.CharField(max_length=100,
blank=True)
lastname = models.CharField(max_length=100,
blank=True)
def __unicode__(self):
return self.user.username
def Parent_Name(self):
try:
return '%s %s' % (adult.firstname,adult.lastname)
except Exception:
return ''
getParentName.shot_description = 'adult'
Admin.py:
class AdultAdmin(admin.ModelAdmin):
list_display = ('Parent_Name', 'Student_Name',)
search_fields = ['Parent_Name',]
admin.site.register(Adult, AdultAdmin)
Error:
Cannot resolve keyword ‘Parent_Name’ into field.
What should be the correct way of writing this so that the function name can be used for search_fields?
The
search_fieldsshould be:You can’t specify your own search function, as it only accepts a list of strings indicating the fields you want to search (unlike the
list_displayoption that you have mentioned which does accept functions – allowing you to programmatically return the value of the list item).You can see the appropriate code that deals with
search_fieldshere (and it’s limitations) on github. The best you can do is follow relationships.