On Mon, Aug 15, 2011 at 4:51 AM, [email protected]
<[email protected]>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/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to