Source Address: https://www.jb51.net/article/140939.htm
Using Scrapy to crawl some of the data on the web, stored in the MySQL database, want to use Django to show the data, see on the web is using the Django models and Makemigration,migrate commands to create a new table, and use.
But my data already exists, has been created, and the data has been stored, no new tables can be recreated.
Knowing the mapping of Django's indication and models names allows Django to use tables that already exist.
If the Django exists models as follows:
fromDjango.dbImportModels#Create your models here.classSciencenews (models. Model): ID= Models. Charfield (max_length=36,primary_key=True) First_module= Models. Charfield (max_length=30,default="News") Second_module= Models. Charfield (max_length=30,default="Latest News") Title= Models. Charfield (max_length=300) Author= Models. Charfield (max_length=60,null=True) Publish_date= Models. Charfield (max_length=35,null=True) Content= Models. TextField (null=True) Crawl_date= Models. Charfield (max_length=35,null=True) From_url= Models. Charfield (Max_length=350,null=true)
To perform a data migration command:
Python manage.py makemigration python manage.py migrate
A data table named Show_sciencenews is generated in the database.
Show is the app name, where my app name is show.
You can see the naming rules for Django-created tables: the name of the application name.
The table name of the data that my store crawled to was originally Science_news and wanted Django to use it instead of creating a new table, just change its name to: Name of the models that you want to map with that table, where I change to show_sciencenews.
Then using the data Migration command as above, you may be prompted for errors that already exist in the datasheet, regardless of the models already mapped to the data table. The next step is to use the models and data sheets normally.
Django uses a data table method that already exists in the MySQL database