I want to display the level field of the category
to which the product
is related on the object\'s admin page.
class Category(m
In your admin.py file
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'category__level', 'category')
admin.site.register(Product, ProductAdmin)
Try this.............
The simplest way is to put the level
of the Category
into the __unicode__
method:
class Category(models.Model):
name = models.CharField(max_length=50, default=False)
level = models.IntegerField(help_text="1, 2 ,3 or 4")
def __unicode__(self):
return u'%s [%d]' % (self.name, self.level)
So the select box will show it.
Define a method on the ModelAdmin class which returns the value of the related field, and include that in list_display
.
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'level')
model = Product
def level(self, obj):
return obj.category.level