青岛制作网站企业,安徽seo报价,网络营销公司推荐,长沙企业建站方案你继承Django的ModelChoiceField并修改它的render_options()和render_option()方法来显示你所需要的对象的属性。我想你也需要你自己的ModelChoiceIterator的子类#xff0c;这样它不仅可以吐出id/label元组#xff0c;而且可以分离出你需要的所有数据。例我只是找到了一个自…你继承Django的ModelChoiceField并修改它的render_options()和render_option()方法来显示你所需要的对象的属性。我想你也需要你自己的ModelChoiceIterator的子类这样它不仅可以吐出id/label元组而且可以分离出你需要的所有数据。例我只是找到了一个自定义的SelectWidget的实施OpenStacks dashboardclass SelectWidget(widgets.Select):Customizable select widget, that allows to renderdata-xxx attributes from choices... attribute:: data_attrsSpecifies object properties to serialize asdata-xxx attribute. If passed (id,),this will be rendered as:option_valuewhere 123 is the value of choice_value.id.. attribute:: transformA callable used to render the display valuefrom the option object.def __init__(self, attrsNone, choices(), data_attrs(), transformNone):self.data_attrs data_attrsself.transform transformsuper(SelectWidget, self).__init__(attrs, choices)def render_option(self, selected_choices, option_value, option_label):option_value force_unicode(option_value)other_html (option_value in selected_choices) and \u selectedselected or if not isinstance(option_label, (basestring, Promise)):for data_attr in self.data_attrs:data_value html.conditional_escape(force_unicode(getattr(option_label,data_attr, )))other_html data-%s%s % (data_attr, data_value)if self.transform:option_label self.transform(option_label)return u%s % (html.escape(option_value), other_html,html.conditional_escape(force_unicode(option_label)))编辑只要你提供这样的选择它应该工作def formfield_for_foreignkey(self, db_field, request, **kwargs):if db_field.name dummy:widget SelectWidget(data_attrs(bar,))choices [(foo.id, foo) for foo in Foo.objects.all()]form_field forms.ChoiceField(choiceschoices, widgetwidget)return form_fieldreturn super().formfield_for_foreignkey(db_field, request, **kwargs)