Django uses a data table method that already exists in the MySQL database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.