2016-05-21 16 views
1

Django 1.9+では、リストをfield_orderに渡すことでModelFormのフィールドの順序を指定できます。私は、フォームのMetaクラスでexcludeを持っている場合しかし、それは動作しません:Djangoフォームフィールドを除外で指定する

class MyForm(ModelForm): 

    class Meta: 
     model = MyModel 
     exclude = ['unwanted_field1', 'unwanted_field2'] 
     field_order = ['foo', 'bar', 'spam', 'eggs'] 

(私は彼らがモデルで定義されている順序でフィールドを取得します)。

class MyForm(ModelForm): 

    class Meta: 
     model = MyModel 
     fields = ['foo', 'bar', 'spam', 'eggs'] 

は、どのように1はfield_orderを使用する必要があります。私が代わりにexcludeを使用しての私が欲しいのフィールドを指定した場合、彼らは私が望むように命じていますか?

答えて

1

他の誰かが同じようにトリップする場合は、私自身の解決策をここに残しておきます。 field_orderModelFormクラスの属性であり、Metaクラスではありません(何らかの理由で説明を歓迎します...)。したがって、次のように動作します。

class MyForm(ModelForm): 

    field_order = ['foo', 'bar', 'spam', 'eggs'] 

    class Meta: 
     model = MyModel 
     exclude = ['unwanted_field1', 'unwanted_field2'] 
関連する問題