0

我有一个模型表单 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
4

1 回答 1

1

只需将fields属性设置为Meta

class PropertyBuildingForm(ModelForm):
    block = forms.CharField(widget = forms.Select())
    class Meta:
        model = PropertyBuilding
        fields = ('block', 'year_built', 'number_of_floors')
于 2013-10-27T07:07:33.707 回答