Reverse generation of database Tables (ii) Django ORM Inspectdb

Source: Internet
Author: User

In the previous article we said that Mybatis-generator generated the code in reverse.

Here we begin to say how to reverse generate MySQL model code in Django.

Before we show the Django ORM reverse build, let's start by saying how to generate the code forward.

Forward generation, which refers to creating a model.py file, and then creating a model.py-compliant table in the database, such as MySQL, through the Django built-in compiler.

Reverse generation, which refers to creating a table in the database first, and then generating the model code through the Django built-in compiler.

1. Preparatory work

Create Django projects and apps

Create a Django project with the name HelloWorld

django-admin.py Startproject HelloWorld

Create an app with the name test

Python manage.py Startapp Hello  

Configuration database

Configuring the database in setting.py

DATABASES = {    'default': {        'ENGINE':'Django.db.backends.mysql',        'NAME':'Big_data',        'USER':'Root',        'PASSWORD':'1234',        'HOST':'10.93.84.53',        'PORT':'3306',    }}

2. Forward generation

Create a model.py in the Hello app directory

 from Import Models class Alarmgroup (models. Model):    = models. Charfield (Primary_key=true, max_length=250)    = models. TextField (blank=true, null=True)    = models. TextField (blank=true, null=True)    = models. Datetimefield ()

To perform a command forward build

Python manage.py makemigrations
Python manage.py Migrate

You can view the table that was created successfully in the configured database

3. Reverse Generation

CREATE table in database now

CREATE TABLE ' Alarm_group ' (  varchar(notNULL,  ' group_des ' blob,  ' Members ' blob,  'timestampdatetimeis notNULL,  ' on_duty ' blob,  ' leader ' blob,  PRIMARYKEY  (' group_name ') ENGINE  =DEFAULT CHARSET=UTF8

Then execute the command to generate the model.py code

Python manage.py Inspectdb

The generated code model.py is as follows

class Alarmgroup (models. Model):    = models. Charfield (Primary_key=true, max_length=250)    = models. TextField (blank=true, null=True)    = models. TextField (blank=true, null=True)    = models. Datetimefield ()    class  Meta:        = False        'alarm_ Group'

The db_table in this map the table names in the database.

Reverse generation of database Tables (ii) Django ORM Inspectdb

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.