E
E
Egor2018-09-12 18:29:31
Django
Egor, 2018-09-12 18:29:31

How to add a field to an existing django model?

I recently started learning Django. I'm using the latest version at the moment 2.1.1 Created several models and migrated. Everything worked fine. Then there was a need to add new fields to the existing model. And when trying to migrate, a lot of errors appeared. Moreover, during the initial creation of models, I simply clicked to save the file, with the server running, and all fields were added on the fly. I tried the way to create a new database, delete the migration folder, create a super user. It worked and everything worked with the new fields. The database clearly had to be filled in by a guest. And now the question. I again need to add a new field to the model without pain. I searched the Internet, found Django South - I didn’t understand what it was, and whether it was relevant. Are there any solutions for this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Zubenko, 2018-09-17
@JawsIk

Agree with Pavel. And if by points, then usually new fields are added with half a kick. These are the changes in the names of the models, there may be ambushes and you need to dance with a tambourine (I opened the database and changed the names of the tables) (and even then I think that these are the features of sqlite and with some MySql, and even more so PostgreSQL, in general, everything would go smoothly ), but in your case you need to really look at the errors.
What have I encountered.
So yes, you need to look at each specific case. The main thing is not to be afraid. By the way!!! I also do such a feint with my ears, like removing a database dump and then filling it back. I did this for two things. Firstly, in order to switch from sqlite to PostgreSQL and not fill the database again and in order to be able to transfer the project and database through Bitbucket (or GitHub) as well.
We merge the database like this (in the terminal):
python manage.py dumpdata --indent=2 --exclude=contenttypes > datadump.json
Then we change the connection (or transfer it to another computer, for example, or do it after creating a new database) (we write 3 commands in turn) :
python manage.py makemigrations
python manage.py migrate --run-syncdb
python manage.py loaddata datadump.json

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question