Django Admin - search_fields + related fields

519 views
Skip to first unread message

Ivan Goncalves

unread,
Feb 20, 2014, 4:28:59 PM2/20/14
to django...@googlegroups.com
I have these two models below: 

class business(models.Model):
    name=models.CharField(max_length=40, blank=True, null=True)
    
class phone(models.Model):
    business=models.ForeignKey(business, verbose_name='Business',blank=False)    
    phone=models.CharField(max_length=40, blank=True, null=True)

in the admin.py

class businessAdmin(admin.ModelAdmin):
    model=business
    search_fields = phone__phone

How do we make this search work fine ? Ie, bring me all companies that possess the phone number entered by the User in the search of the form (Admin). ?


Camilo Torres

unread,
Feb 21, 2014, 4:00:31 PM2/21/14
to django...@googlegroups.com
 Hello,

search_fields is a list of names (strings), so you must put:

search_fields = ['phone__phone']

You can also add more field names to this list for the search to include those fields. See your same example, working, here:

Regards,
Camilo 
Reply all
Reply to author
Forward
0 new messages