SoFunction
Updated on 2024-10-29

django implementations to write modified new models to the database

We use django to write the model code, surely can not wait to translate the model for the migration file and then migrate it, and later found that the model to modify the words and then python makemigrations app plus migrate the results found that it does not matter, the database will not make any modifications, due to the fact that I am also a novice, and so on the Internet! As I am also a novice, so on the Internet to look at the next information, and analyzed, found a rough solution:

(1) Delete the original migration file

Catalog the following project name.

(2) Re-establishment of migration documents

i.e. re-python makemigrations app

(3) Deletion of database records

Go to the project database, find the django_migrations table, and delete the row where app is the name of your app.

(4) Delete the old table of the relevant app

Delete all the tables about the app you modified.

(5) Add the modified table to the database

Come back to migrate and you will find that your modified model has been successfully imported into the database.

If any of you gods have an even better method, feel free to share!

Additional knowledge:python Django Initialize Data to Database Operation

Introduction: the development will often encounter some data in the program's runtime to exist in the database, and then on Django's data initialization to do a simple sharing

1. First of all in our app under the creation of the corresponding models, and then inside the models for the creation of the table

2. Corresponding to the table above, we would like to initialize this table with two pieces of data

name:1 script_content:‘11111'

name:2 script_content:‘22222'

3. Then perform the usual table structure creation operations, run python makemigrations + your app name at the command line

4. Then you will see the creation of a migrations directory in the corresponding app

5. Initialize your own data into the database according to your own migration file, then run, at the command line, python makemigrations --enmpty + your own app name.

6. Then a file will be generated in the migrations directory as shown in the figure below, starting with 0002:

7. Then in the above newly generated file inside the writing of the two functions of code

Then add to the file

Then at the command line, run, python migrate

At this point there is initialization data in the database

Roughly the whole process looks like this, complete with initialization of the django data

Above this django implementation will be modified to write the new model to the database is all I have shared with you, I hope to be able to give you a reference, and I hope you support me more.