Using the Django admin_display Property

I have models and their admin code below. The question is, how can I display the first three tag books in the list_display property? I can show tags while editing a book, but I would like its tags to be in tags when the book is 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 some suggestion? Thanks

+6
source share
1 answer

Use your own method in the admin class.

 class BookAdmin(admin.ModelAdmin): list_display = ('name', 'three_tags') def three_tags(self, obj): return obj.booktag_set.all()[:3] 
+14
source

Source: https://habr.com/ru/post/898762/


All Articles