解决方案https://stackoverflow.com/a/18475322/892040从ModelForm
. 但是,这确实有效:
class ItemReview(ModelForm):
class Meta:
model = Item
_field_order = ['field1', 'field2']
def __init__(self, *args, **kwargs):
super(ItemReview, self).__init__(*args, **kwargs)
field_order = getattr(ItemReview, '_field_order')
visited = []
if field_order:
new_fields = OrderedDict()
for field_name in field_order:
if field_name in self._fields:
new_fields[field_name] = self._fields[field_name]
visited.append(field_name)
for field_name in self._fields:
if field_name in visited:
continue
new_fields[field_name] = self._fields[field_name]
self._fields = new_fields