Django 1.9添加了一个新
Form属性,
field_order无论字段在类中的声明顺序如何,都可以对字段进行排序。
class MyForm(forms.Form): summary = forms.CharField() description = forms.CharField(widget=forms.textarea) author = forms.CharField() notes = form.CharField() field_order = ['author', 'summary']
缺少的字段
field_order按其顺序排列在类中,并附加在列表中指定的字段之后。上面的示例将按以下顺序生成字段:
['author','summary', 'description', 'notes']
请参阅文档:https : //docs.djangoproject.com/en/stable/ref/forms/api/#notes-
on-field-
ordering
我遇到了同样的问题,我发现了另一种在Django
CookBook中对字段重新排序的技术:
class EditForm(forms.Form): summary = forms.CharField() description = forms.CharField(widget=forms.textarea)class CreateForm(EditForm): name = forms.CharField() def __init__(self, *args, **kwargs): super(CreateForm, self).__init__(*args, **kwargs) self.fields.keyOrder = ['name', 'summary', 'description']
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)