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
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]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With