Rego Rego - 1 year ago 112
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,

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
model where i want to provide list display for both
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

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

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

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

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

Answer Source

I think you have to do

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