Rego Rego - 3 months ago 17
Python Question

django change list display column name with foreign field value

I am using

django 1.9
, i have following model which have two foreing fields,

models.py

class Mobile(models.Model):
name = models.CharField(max_length=100)
product = models.ForeignKey(Product)
owner = model.ForeignKey(Customer)


now i want to build a admin interface for
Mobile
model where i want to provide list display for both
product
and
owner
foreign fields,that is, the list display will show one of the field of that foreign fields and also want to change the column name,i have tried so but its not working at all

admin.py

class MobileModelAdmin(admin.ModelAdmin):
list_display = ('name','product', 'owner')

def related_product(self, obj):
return obj.product.name
related_product.short_description = 'product name'

def related_owner(self, obj):
return obj.owner.name
related_owner.short_description = 'owner name'

admin.site.register(Mobile, MobileModelAdmin)


but neither its changing the column name nor showing the related value.

Answer

I think you have to do

list_display = ('name','related_product', 'related_owner') #means method name what you are given.
Comments