showing an attribute of a foreign key in admin

1,225 views
Skip to first unread message

in...@webbricks.co.uk

unread,
Aug 15, 2011, 4:51:35 AM8/15/11
to Django users
copied and pasted from stackoverflow, since i didnt get an answer in a
week and i just can't believe its not possible.

I want to display the Level of the Category that the product belongs
to, in the admin page for the Product. snipped a lot of the
unimportant fields out of the display below.

class Category(models.Model):
name = models.CharField(max_length=50, default=False)
level = models.IntegerField(help_text="1, 2 ,3 or 4")

class Product(models.Model):
category = models.ForeignKey(Category)
name = models.CharField(max_length=100)


prepopulated_fields = {'slug': ('name',)}
fieldsets = [
('Product Info',{'fields': ['name',
'slug','partno','description']}),
('Categorisation',{'fields': ['brand','category']}),


obviously i've tried a little to get this working and googled a lot,
but i've found reference to list_filter lots, but nothing about just
showing the field. best guess was
'category__level' inside the fieldset as a field, but that didnt work.

anyone know the right way to do this?

Daniel Roseman

unread,
Aug 15, 2011, 5:15:27 AM8/15/11
to django...@googlegroups.com

Karen Tracey

unread,
Aug 15, 2011, 7:36:53 AM8/15/11
to django...@googlegroups.com
On Mon, Aug 15, 2011 at 4:51 AM, in...@webbricks.co.uk <in...@webbricks.co.uk> wrote:

I want to display the Level of the Category that the product belongs
to, in the admin page for the Product. snipped a lot of the
unimportant fields out of the display below.

class Category(models.Model):
   name = models.CharField(max_length=50, default=False)
   level = models.IntegerField(help_text="1, 2 ,3 or 4")

class Product(models.Model):
   category = models.ForeignKey(Category)
   name = models.CharField(max_length=100)


   prepopulated_fields = {'slug': ('name',)}
   fieldsets = [
       ('Product Info',{'fields': ['name',
'slug','partno','description']}),
       ('Categorisation',{'fields': ['brand','category']}),


You can do this so long as you only want to display the level, and not change it from the Product edit page. Admin was not designed to allowed editing of related-model attributes so you cannot do that. But with readonly fields you can easily display information:

1 - Define a method on Product that returns the information you want, eg:

    def category_level(self):
        try:
            return u'%s' % self.category.level
        except Category.DoesNotExist:
            return u'No Category'

(You need to allow for Category not existing since it will not exist on an add page.)

2 - Include that method in readonly_fields and wherever you want it in your fieldsets, eg:

class ProductAdmin(admin.ModelAdmin):
   readonly_fields = ['category_level']
   fieldsets = [
       ('Product Info',{'fields': ['name', etc etc etc ]}),
       ('Categorisation',{'fields': ['category', 'category_level']}),
    ]

Karen
--
http://tracey.org/kmt/

in...@webbricks.co.uk

unread,
Aug 16, 2011, 5:56:29 AM8/16/11
to Django users
thanks tracy, i'd tried something very similar to Daniels method and
failed, but your code worked first time.

On Aug 15, 12:36 pm, Karen Tracey <kmtra...@gmail.com> wrote:
> On Mon, Aug 15, 2011 at 4:51 AM, i...@webbricks.co.uk
> <i...@webbricks.co.uk>wrote:
Reply all
Reply to author
Forward
0 new messages