Django database table reverse generation instance resolution, django instance
 
This article describes how to reverse generate mysql model code in django. Next, let's take a look at the details.
 
Before presenting the reverse generation of django ORM, let's talk about how to generate code in a forward direction.
 
Forward generation refers to creating a model. py file first, and then creating a table that complies with model. py in a database such as mysql through the built-in django compiler.
 
Reverse generation refers to first create a table in the database, and then generate the model Code through the built-in django compiler.
1. Preparations 
Create a django project and an app
 
Create a django project named helloworld
 
django-admin.py startproject helloworld
 
Create an app named test
 
python manage.py startapp hello 
 
Configure Database
 
Configure the app in INSTALLED_APPS of settings. py.
 
# Application definition  INSTALLED_APPS = [   'django.contrib.admin',   'django.contrib.auth',   'django.contrib.contenttypes',   'django.contrib.sessions',   'django.contrib.messages',   'django.contrib.staticfiles',   'hello', ]
 
Configure the database in settings. 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 model. py in the hello app directory
 
from django.db import modelsclass 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()
 
Execute the command to generate a forward
 
python manage.py makemigrationspython manage.py migrate
 
You can go to the configured database to view successfully created tables.
3. Reverse generation 
Create a table in the database
 
CREATE TABLE `alarm_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
 
Then run the command to generate the model. py code.
 
python manage.py inspectdb
 
The generated code model. py is 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'
Summary 
The above is all about reverse generation of instance parsing in the Django database table in this article. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!