[Solved]-Reordering fields in Django model

5đź‘Ť

âś…

If you mainly need the ordering for Django’s admin you could also create your “generic”-admin class via sub-classing Django’s admin class. See http://docs.djangoproject.com/en/dev/intro/tutorial02/#customize-the-admin-form for customizing the display of fields in the admin.
You could overwrite the admin’s __init__ to setup fields/fieldsets on creation of the admin instance as you wish. E.g. you could do something like:

class MyAdmin(admin.ModelAdmin):
    def __init__(self, model, admin_site):
        general_fields = ['notes', 'created_at', 'updated_at']
        fields = [f.name for f in self.model._meta.fields if f.name not in general_fields]
        self.fields = fields + general_fields
        super(admin.ModelAdmin, self).__init__(model, admin_site)

Besides that i think it’s not a good practice to modify the (private) _field_name_cache!

7đź‘Ť

I was having the very same problem, but I found these solutions to be problematic, so here’s what I did:

class BaseAdmin(admin.ModelAdmin):
    def get_fieldsets(self, request, obj = None):
        res = super(BaseAdmin, self).get_fieldsets(request, obj)
        # I only need to move one field; change the following
        # line to account for more.
        res[0][1]['fields'].append(res[0][1]['fields'].pop(0))
        return res

Changing the fieldset in the admin makes more sense to me, than changing the fields in the model.

👤pgcd

2đź‘Ť

I ALSO didn’t like the other solutions, so I instead just modified the migrations files directly.

Whenever you create a new table in models.py, you will have to run “python manage.py makemigrations” (I believe this in Django >= v1.7.5). Once you do this, open up the newly created migrations file in your_app_path/migrations/ directory and simply move the rows to the order you want them to be in. Then run “python manage.py migrate”. Voila! By going into “python manage.py dbshell” you can see that the order of the columns is exactly how you wanted them!

Downside to this method: You have to do this manually for each table you create, but fortunately the overhead is minimal. And this can only be done when you’re creating a new table, not to modify an existing one.

👤jball037

Leave a comment