Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Columns ordering in Django's model

So, I am wondering that how to define the order of the columns in Model so that when Django does forward engineering to generate database, the order of columns in the physical database table can be determined by the Model itself?

Currently some of my model classes inhering from other model class, so the derived model class always put the columns from the parent class first, but I want to control that.

Thanks

like image 804
zs2020 Avatar asked Nov 11 '10 20:11

zs2020


2 Answers

On Django 1.8 this worked for me:

  • run ./manage.py makemigrations
  • edit the migration
  • reorder the fields list on migrations.CreateModel.

I did not test this with inheritance though.

like image 156
felixhummel Avatar answered Oct 16 '22 02:10

felixhummel


Currently, there's no built-in way to do this in Django.

I seem to remember a django-dev thread/ticket that discussed this, but I can't see to find it at the moment. I'll update this if I find it (and welcome anyone else to if they do.)

There's probably a way to hack this into the core if you really need it that bad.

like image 41
Sam Dolan Avatar answered Oct 16 '22 02:10

Sam Dolan