Django: a more friendly title for StackedInline for an auto-generated model?

I am using Django admin StackedInline as below:

class BookInline(admin.StackedInline): model = Book.subject.through verbose_name = 'Book' verbose_name_plural = 'Books with this subject' class SubjectAdmin(admin.ModelAdmin): inlines = [ BookInline, ] 

Everything works, but the title is pretty ugly:

 Books With This Subject Book: Book_subject object 

Does anyone know how I can get rid of the Book_subject object part or change it?

thanks!

+6
python django django-admin
source share
2 answers

I have never used an m2m field like this, so thanks! I learned something new.

I found 2 ways to get around the problem:

1: just reassign the __unicode__ function __unicode__ new function

 class MyInline(admin.TabularInline): MyModel.m2m.through.__unicode__ = lambda x: 'My New Unicode' model = MyModel.m2m.through 

2: configure the proxy model for m2m.through model and use this model instead

 class MyThrough(MyModel.m2m.through): class Meta: proxy = True def __unicode__(self): return "My New Unicode" class MyInline(admin.TabularInline): model = MyThrough 
+8
source share

For some reason, the accepted answer (admittedly now old) didn't work for me.

This modification, however, changed the title:

 MyModel.field.through.__str__ = lambda x: 'New Title' 

Where field is the ManyToMany field.

0
source share

All Articles