我在 list_display 中添加了一个小函数来显示多对多字段,我正在尝试显示由换行符分隔的 m2m 对象。 但是换行符不起作用
我的 django 管理员:
class ArticleAdmin(admin.ModelAdmin):
list_filter = ["category",]
list_display = ('category','article_type', 'get_domains')
filter_horizontal = ("domain",)
def get_domains(self, obj):
x ="\n".join([str(s.name) for s in obj.domain.all()])
print x # printing fine
return x
get_domains.short_description = 'Domains'
打印语句很好:
0 errors found
Django version 1.4.5, using settings 'settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
mykitchenremodelinglosangeles.com:8000
kitchenremodelerchicagoil.com:8000
localhost:8000
我附上了管理屏幕输出:
你能推荐这个吗?
最佳答案
HTML 中的浏览器会忽略新行。如果你想在不同的行中显示它们,我认为你应该 join
他们与<br>
而不是 \n
并使用 allow_tags
.
class ArticleAdmin(admin.ModelAdmin):
list_filter = ["category",]
list_display = ('category','article_type', 'get_domains')
filter_horizontal = ("domain",)
def get_domains(self, obj):
x ="<br>".join([str(s.name) for s in obj.domain.all()])
print x # printing fine
return x
get_domains.allow_tags = True
希望这对您有所帮助!
关于python - Django 管理更改列表页面中未选择新行 (\n),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17278834/