首页 > 代码库 > django 有model生成SQL以及现有反向表生成model

django 有model生成SQL以及现有反向表生成model

已有models生成SQL语句

语法 python manage.py sqlall app_name   # app_name, 在settings已经导入,

如:

INSTALLED_APPS = (
‘django.contrib.admin‘,
‘django.contrib.auth‘,
‘django.contrib.contenttypes‘,
‘django.contrib.sessions‘,
‘django.contrib.messages‘,
‘django.contrib.staticfiles‘,
"apps.libs.models.weixin",
)

python manage.py sqlall weixin

wei-2:tbkw (develop)admin$ python manage.py sqlall weixin
{DB_JX_MASTER_HOST: mysql.rds.aliyuncs.com, DB_JX_PORT: 3306, DB_JX_USER: tb, DB_JX_PASSWORD: tb}
BEGIN;
CREATE TABLE `wechat_config` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `user_id` integer NOT NULL,
    `app_id` varchar(50) NOT NULL,
    `app_secret` varchar(50) NOT NULL,
    `app_token` varchar(100) NOT NULL,
    `access_token` varchar(200) NOT NULL,
    `access_token_expire_in` integer,
    `add_time` datetime(6) NOT NULL
)
;
CREATE TABLE `wechat_menu` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `wechat_id` integer NOT NULL,
    `menu_name` varchar(50) NOT NULL,
    `menu_type` varchar(20) NOT NULL,
    `menu_key` varchar(50) NOT NULL,
    `menu_value` varchar(50) NOT NULL,
    `is_button` integer NOT NULL,
    `level_id` integer NOT NULL,
    `parent_id` integer,
    `sequence` integer NOT NULL
)
;
CREATE TABLE `wechat_autoreply` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `wechat_id` integer NOT NULL,
    `replay_type` integer NOT NULL,
    `keyword` varchar(100) NOT NULL,
    `is_welcome` integer NOT NULL,
    `unmatch` integer NOT NULL,
    `text` varchar(1000) NOT NULL,
    `media` varchar(100) NOT NULL,
    `add_time` datetime(6) NOT NULL
)
;
CREATE TABLE `wechat_material` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `wechat_id` integer NOT NULL,
    `title` varchar(100) NOT NULL,
    `author` varchar(100) NOT NULL,
    `content` longtext NOT NULL,
    `source_url` varchar(100) NOT NULL,
    `cover` varchar(100) NOT NULL,
    `cover_show` integer NOT NULL,
    `summary` varchar(1000) NOT NULL,
    `add_time` datetime(6) NOT NULL
)
;
CREATE TABLE `wechat_msg_push` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `wechat_id` integer NOT NULL,
    `is_push` integer NOT NULL,
    `template_id` integer NOT NULL,
    `title` varchar(100) NOT NULL,
    `content` longtext NOT NULL,
    `add_time` datetime(6) NOT NULL
)
;

$ python manage.py makemigrations weixin

 

 

有现有表反向生成models

语法 python manage.py inspectdb

wei-2:tbkw (develop)admin$ python manage.py inspectdb
{DB_JX_MASTER_HOST: mysql.rds.aliyuncs.com, DB_JX_PORT: 3306, DB_JX_USER: ‘tb, DB_JX_PASSWORD: ‘tb}
# This is an auto-generated Django model module.
# You‘ll have to do the following manually to clean this up:
#   * Rearrange models‘ order
#   * Make sure each model has one field with primary_key=True
#   * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table
# Feel free to rename the models, but don‘t rename db_table values or field names.
#
# Also note: You‘ll have to insert the output of ‘django-admin sqlcustom [app_label]‘
# into your database.
from __future__ import unicode_literals

from django.db import models


class AuthUser(models.Model):
    username = models.CharField(unique=True, max_length=30)
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.CharField(max_length=75)
    password = models.CharField(max_length=128)
    is_staff = models.IntegerField()
    is_active = models.IntegerField()
    is_superuser = models.IntegerField()
    last_login = models.DateTimeField(blank=True, null=True)
    date_joined = models.DateTimeField()
    logins = models.IntegerField()
    phone = models.CharField(max_length=11, blank=True, null=True)
    qq = models.CharField(max_length=13, blank=True, null=True)

    class Meta:
        managed = False
        db_table = auth_user


class BankAccount(models.Model):
    username = models.CharField(max_length=50)
    amount = models.DecimalField(max_digits=11, decimal_places=2)
    balance = models.DecimalField(max_digits=8, decimal_places=2)
    add_date = models.IntegerField()

    class Meta:
        managed = False
        db_table = bank_account

 

.

django 有model生成SQL以及现有反向表生成model