Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django admin list_display property usage

I have models and their admin code at below. The question is how can I show the first three tag of a book in its list_display property ? I can show the tags while the book is editing but I would like to its 3 tags while the book are listed in the admin panel.

models.py

class Book(models.Model):
    name = models.CharField(max_length=1000)

    def __unicode__(self):
        return self.name

class BookTag(models.Model):
    name = models.CharField(max_length=1000)
    book = models.ForeignKey(Book,null=False,blank=False)    
    def __unicode__(self):
        return self.name

admin.py

class BookTagInline(admin.TabularInline):
    model = JobTitleTag

class BookAdmin(admin.ModelAdmin):
    list_display = ('name')
    inlines = [ BookTagInline, ]

admin.site.register(Book,BookAdmin)

Could you give me any suggestion ? Thanks

like image 939
brsbilgic Avatar asked Oct 06 '11 12:10

brsbilgic


1 Answers

Use a custom method on the admin class.

class BookAdmin(admin.ModelAdmin):
    list_display = ('name', 'three_tags')

    def three_tags(self, obj):
        return obj.booktag_set.all()[:3]
like image 191
Daniel Roseman Avatar answered Sep 19 '22 12:09

Daniel Roseman