本文介绍了Django:通过模型自动生成StackedInline的Friendlier标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django管理StackedInline,如下所示:

  class BookInline(admin.StackedInline):
model = Book.subject.through
verbose_name ='Book'
verbose_name_plural ='具有此主题的书籍'

class SubjectAdmin(admin.ModelAdmin):
inlines = [
BookInline,
]

一切正常,但标题是相当丑陋:

 具有此主题的图书
图书:Book_subject对象

任何人都知道如何摆脱或更改 Book_subject对象 part? p>

谢谢!

解决方案

我从来没有使用像这样的m2m字段, 那谢谢啦!学习了一些新的东西。



我发现有两种方法来解决问题:



1:只需重新分配 __ unicode __ 具有新功能的函数

  class MyInline(admin.TabularInline) 
MyModel.m2m.through .__ unicode__ = lambda x:'我的新Unicode'
model = MyModel.m2m.through

2:设置一个,并使用该模型

 类MyThrough(MyModel.m2m.through )
class Meta:
proxy = True
def __unicode __(self):
返回我的新Unicode

class MyInline(admin.TabularInline ):
model = MyThrough


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

class BookInline(admin.StackedInline):
    model = Book.subject.through
    verbose_name = 'Book'
    verbose_name_plural = 'Books with this subject'

class SubjectAdmin(admin.ModelAdmin):
    inlines = [
        BookInline,
    ]

It all works, but the header is pretty ugly:

Books With This Subject
Book: Book_subject object

Anyone know how I can get rid of, or change, the Book_subject object part?

thanks!

解决方案

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

这篇关于Django:通过模型自动生成StackedInline的Friendlier标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 23:45