Django: Friendlier header for StackedInline for auto generated through model?

前端 未结 2 982
小蘑菇
小蘑菇 2020-12-16 01:47

I\'m using a Django admin StackedInline, as follows:

class BookInline(admin.StackedInline):
    model = Book.subject.through
    verbose_name = \'Book\'
            


        
相关标签:
2条回答
  • 2020-12-16 02:02

    I've never used an m2m field like this, so thanks! Learned something new.

    I found 2 ways to get around the problem:

    1: simply reassign the __unicode__ function with a new function

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

    2: set up a proxy model for the m2m.through model and use that model instead

    class MyThrough(MyModel.m2m.through):
        class Meta:
            proxy = True
        def __unicode__(self):
            return "My New Unicode"
    
    class MyInline(admin.TabularInline):
        model = MyThrough
    
    0 讨论(0)
  • 2020-12-16 02:08

    For some reason, the (admittedly now old) accepted answer did not work for me.

    This modification, however, did change the header:

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

    Where field is the ManyToMany field.

    0 讨论(0)
提交回复
热议问题