Django database migration and django migration
We have compiled the code for the blog database model, but it is only Python code. Django has not translated it into a database language, therefore, these database tables are not actually created in the database.
In order for Django to complete the translation and create these database tables, we once again asked my project management assistant manage. py.Activate Virtual EnvironmentSwitch to the directory where the manage. py file is located and runpython manage.py makemigrations
Andpython manage.py migrate
Command. After the first command is run, a 000w.initial.py file is generated under the migrations \ directory of the blog application. This file is a file that Django uses to record the modifications we made to the model. Currently, three model classes are created in the models. py file. Django records these changes in 000w.initial.py.
When you enter the first command (the model data model of the application blog has been created in setting)
After Baidu, I tried the following:
Operate databases using Django
Store Data (this opens an interactive command line. First, we create a category and a tag, import three previously written model classes, and instantiateCategory
Class andTag
Class for their attributesname
Assigned value. In order for Django to save the data to the databasesave
Method .)
Try creating another article, but before creating an article, we need to create a User to specify the author of the article. The User Creation command Django has been written for us and is still running through manage. py. First, press Ctrl + c to exit the command interaction bar (several times in a row if one exit fails) and runpython manage.py createsuperuser
Command and follow the prompts to create a user: (username and password are 8 characters)
Run againpython manage.py shell
Go to the Python command interaction bar and create an article: