ModelMultipleChoiceField

ModelMultipleChoiceField

本文介绍了django:自定义ModelMultipleChoiceField的显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ModelMultipleChoiceField显示在模板中,是具有对应对象的表示形式的unicode的复选框列表。如何以任意列的任意字段在表格中显示ModelMultipleChoiceField?例如:



[x] | obj.name | obj.field1

解决方案

字段类有一个方法 label_from_instance 如何表示对象。您可以在自己的字段类中覆盖它:

 从django.forms.models import ModelMultipleChoiceField 

MyMultipleModelChoiceField(ModelMultipleChoiceField):

def label_from_instance(self,obj):
返回%s |& s%(obj.name,obj.field1)

您还应该可以输出一些html ...


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.name | obj.field1

解决方案

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)

You should also be able to output some html with that...

这篇关于django:自定义ModelMultipleChoiceField的显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 21:19