Django get form not ordered list
<p>i changed my admin form when creating new objects to hide some fields, but it orders fields alphabetically, i want to order them as they are in my model.any suggestions?</p>

<pre><code>_add_fields = ('name', 'size', 'slug',
               'img', 'description', 'quantity')

def get_form(self, request, obj=None, **kwargs):
    model_form = super(ItemAdmin, self).get_form(
        request, obj, **kwargs
    )

    if obj is None:
        model_form._meta.fields = self._add_fields
        model_form.base_fields = {
            field: model_form.base_fields[field]
            for field in self._add_fields
        }

    return model_form
</code></pre>

<p>You need to use <code>OrderedDict</code> from <code>collections</code> module to achieve that:</p>

<pre><code>from django.contrib import admin
from collections import OrderedDict


class ItemAdmin(admin.ModelAdmin):

    _add_fields = ('name', 'category',
                   'img', 'description', 'quantity')

    def get_form(self, request, obj=None, **kwargs):
        model_form = super(ItemAdmin, self).get_form(
            request, obj, **kwargs
        )

        if obj is None:
            model_form._meta.fields = self._add_fields
            d = OrderedDict()
            for field in self._add_fields:
                d[field] = model_form.base_fields[field]
            model_form.base_fields = d
        return model_form
</code></pre>

python django django-admin