问题描述
ModelMultipleChoiceField 显示在模板中,是一个复选框列表,带有对应对象的 Unicode 表示.如何使用任意列中的任意字段以表格形式显示 ModelMultipleChoiceField?例如:
ModelMultipleChoiceField is displayed in a template is a list of checkboxes with unicode of representation of corresponding objects. How do I display ModelMultipleChoiceField in table form with arbitrary fields in arbitrary columns? For example:
[x] |对象名称 |obj.field1
[x] | obj.name | obj.field1
推荐答案
字段类有一个方法 label_from_instance
控制对象的表示方式.您可以在自己的字段类中覆盖它:
The field class has a method label_from_instance
that controls how the object is represented. You can overwrite it in your own field class:
from django.forms.models import ModelMultipleChoiceField
class MyMultipleModelChoiceField(ModelMultipleChoiceField):
def label_from_instance(self, obj):
return "%s | %s" % (obj.name, obj.field1)
你也应该能够输出一些 html...
You should also be able to output some html with that...
这篇关于django:自定义 ModelMultipleChoiceField 的显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!