100. ORM model is generated based on the existing table

The automatically generated model existing table:

1. In the actual development of some of the site is developed by php, etc., after you use django development, then, some tables already exist, so we're going through some commands to the database table to generate the corresponding lot orm model.

2. Specific steps to be executed:

(1) Work on the database connection configured in settings.py file in the project:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'table_orm_demo02',
        'USER': 'root',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',
        'PORT': '3306'
    }
}
(2) In the terminal command line window into the project where the virtual environment: workon virtual environment name (table_orm_demo02). In the terminal go to the directory where the project will generate a table ORM model already exists in the database, execute the command: python manage.py inspectdb. The ORM model mapped to the generated table models.py project file, execute the command:. Python manage.py inspectdb> models.py This will generate the corresponding model ORM models.py project file, may then respective model information classified into different app model, followed by modify the relationship between the ORM model, if the difference between a foreign key field is used in the form of associated, then the reference to app as a string transfer, if following this form, and if ORM model referenced in the following reference will be unsuccessful this model, as follows:
class BookOrder(models.Model):
    price = models.FloatField()
    book = models.ForeignKey(Book, models.DO_NOTHING)
    # 可以修改为:将引用的模型作为字符串的形式
    book = models.ForeignKey(Book, models.DO_NOTHING)
    
class Book(models.Model):
    name = models.CharField(unique=True, max_length=100)
    pages = models.IntegerField()
Note: If you just want to convert to a table model, then you can specify the name of the table, an example command is as follows:
python manage.py inpectdb article > models.py
(3) Another thing to note is this: When mapping during model, will generate a property managed = False Meta class in each of the ORM model, this must be removed, whether those words, django will not manage to generate the ORM model.
(4) in the definition of the foreign key when the table when the ORM model generated in the foreign key references are deleted, the processing done is: models.DO_NOTHING, this can be changed according to the actual needs of the project.
(5) can modify the name of the ORM model generated, but can not modify the name of the table, if you modify the table name, and can not correspond to tables in the database, it is also necessary to modify the table name in the database.
(6) execute the command: python manage.py makemigrations generate initialization migration scripts, easy to manage table behind by ORM. This time required Run python manage.py migrate {appname} --fake-initial migration script is mapped to the generated record in the database table, django-migrations recorded in the database, either after normal use python manage .py makemigrations and python manage.py migrate up.
(7) will be mapped to Django's core database table: Django there is also a need to create some tables, if Django is to use previously developed this database, these tables already exist, you can not control. If the database is not used before the Django development, then it should be mapped to Django's core model to the database using the migrate command.
Published 131 original articles · won praise 30 · views 8386

Guess you like

Origin blog.csdn.net/zjy123078_zjy/article/details/104231959