in this article we look at how to generate MySQL model code reverse in Django, and let's look at it in detail.

, before we show the reverse generation of Django ORM, let's start with how to generate the code forward.

is generated forward, referring to creating model.py files first, and then creating a table that matches model.py by using Django built in compiler.

is generated reverse, which refers to the first create table in the database, and then through the Django built-in compiler, the model code is generated.

1,

Django to create app

project and create a Django project, helloworld

 django-admin.py startproject helloworld

is the name of the

to create app name is test

 Python manage.py startapp Hello /pre> 

<

configuration database in settings.py INSTALLED_APPS app

 Application definition INSTALLED_APPS configuration # = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions'.'django.contrib.messages','django.contrib.staticfiles','hello',]

in settings.py

 DATABASES configuration database: = {'default' {'ENGINE':'django.db.backends.mysql' 'NAME','big_data','USER'::'root','PASSWORD':'1234','HOST':'10.93.84.53','PORT':'3306', 

2,}

}

create model.py

  generated positive from django.db import models class AlarmGroup hello in the app directory (models.Model): group_name = models.CharField (primary_key=True, max_length=250) group_des = models.TextField (blank=True, null=True) members = models.TextField (blank=True, null=True) timestamp = models.DateTimeField (

)

 executive command generation Python manage.py makemigrations is Python manage.py migrate

to the configuration database, see the successful creation of table

and reverse

3 generated

now in the database of

 CREATE TABLE `alarm_ create table Group` (`group_name` varchar (250) NOT NULL, `group_des` blob, `members` blob, `timestamp` datetime, NOT NULL, `on_duty` blob, `leader` blob, PRIMARY KEY (`group_name`)) ENGINE=MyISAM DEFAULT CHARSET=utf8

and then execute the command,

 Python manage.py model.py code generation inspectdb

model.py

 generated code as follows: class AlarmGroup (models.Model) group_name = models.CharField (primary_key=True, max_length=250) group_des = models.TextField (blank=True, null=True) members = models.TextField (blank=True, null=True) timestamp = models.DateTimeField (class) Meta: managed = False db_table ='alarm_group'

summarized above is the article on the Django database instance generation reverse The full content of the analysis, I hope to help you. Interested friends can continue to refer to other relevant topics of the station, if there are shortcomings, welcome the message. Thank you for the support of our friends!

This paper fixed link:http://www.script-home.com/django-database-table-reverse-generation-of-instance-parsing.html | Script Home | +Copy Link

Article reprint please specify:Django database table reverse generation of instance parsing | Script Home

You may also be interested in these articles!