Adding New Columns to a Django Model

I created a sample Django application with several models inside it and filled with data.

Now I need to add a new column to one of the models?

Here are my problems?

  • What happens if I do syncdb after adding a column to the model, just change the table and add a new column?
  • Or will he create a new table after deleting all the columns?

Is there a better way to solve this problem?

+1
source share
3 answers

syncdb does not work to modify database tables. Here is the documentation (Readup on: Syncdb will not modify existing tables)

A clean way to achieve this would be to use a third-party tool, such as django south , that would handle migrations (process the table alter scripts in your case).

Below is a step-by-step tutorial in the south, and here is the official documentation in the south

+3
source

syncdb will not add a new column, and if the table already exists, it will not create a new table. the thing i used is just to add the field name to your model. enter the shell and type:

 $ python manage.py dbshell you will get directly within your database shell (mysql or psql) it up to what database you are using. mysql> | psql> ALTER TABLE <table_name> ADD column varchar(100); and it will add the new column to your table, doesn't matter if the table it already populated or not. 
+2
source

In development, I often use the reset function. This is useful to me as I don't mind deleting data. It basically creates new database tables just for this application - so it will delete your data. Not useful if you want the data to be populated, south is better as above.

 python manage.py reset <app-name> 
0
source

Source: https://habr.com/ru/post/1495824/


All Articles