如何订购在Django中动态创建的Model表单字段

时间:2013-10-27 06:58:16

标签: django django-forms

我有一个modelform PropertyBuildingForm

我已经添加了一个额外的字段(块)。此字段按照PropertyBuildingForm的顺序最后。如何更改此字段的顺序?

我已经看到了这个问题Form Field order。但它没有显示这种具体情况。我希望块字段首先出现。

class PropertyBuildingForm(ModelForm):
    block = forms.CharField(widget = forms.Select())
    class Meta:
        model = PropertyBuilding
        exclude = ('property','parcel','images')
    def __init__(self,*args,**kwargs):

        super( PropertyBuildingForm, self).__init__(*args, **kwargs)
        self.fields['year_built'].widget.attrs['class'] = "datetimepicker"       
        self.fields['number_of_floors'].required = True        
        self.fields['block'].required = False
        self.fields['block'].widget.attrs['class'] = "sez-block-building"

    def clean_block(self):
        block = self.cleaned_data['block']      
        return block

1 个答案:

答案 0 :(得分:1)

只需在fields中设置Meta属性:

class PropertyBuildingForm(ModelForm):
    block = forms.CharField(widget = forms.Select())
    class Meta:
        model = PropertyBuilding
        fields = ('block', 'year_built', 'number_of_floors')