开发有时需要动态创建表,创建完成后需要动态选择model对应的表,该需求如何实现
1、model层 TestBlock为了动态创建表、getBlockModel为了动态选择表
from django.db import models # Create your models here. class TestBlock(models.Model): BLOCK_ID = models.CharField(max_length=40,primary_key=True, verbose_name='对象ID') BLOCK_NAME = models.CharField(max_length=200, verbose_name='姓名') class Meta: db_table = 'TEST_BLOCK_T' def getBlockModel(table_name): class MyCLass(models.Model): BLOCK_ID = models.CharField(max_length=40, primary_key=True, verbose_name='对象ID') BLOCK_NAME = models.CharField(max_length=200, verbose_name='姓名') class Meta: db_table = table_name return MyCLass
2、url
from django.urls import path from . import views urlpatterns = [ path('autoCreate', views.createTest), path('autoSelect', views.selectTest), ]
3、view层createTest方法 重点的地方 注意红色地方,注释掉可以动态创建表,但是如果不注释掉,动态变更实体对应的数据表在使用它,就会报错
ERROR:django.db.utils.OperationalError: (1054, "Unknown column 'TEST_BLOCK_T_1.BLOCK_ID' in 'where clause'")
分别注释 #-------------1-begin-------------------------- 和 # -------------2-begin--------------------------之间的代码,执行代码动态创建两个表并把数据插入进去。
from django.shortcuts import render # Create your views here. from .models import * from django.http import HttpResponse from django.db import connection from django.db.backends.base.schema import BaseDatabaseSchemaEditor def createTest(request): #-------------1-begin-------------------------- TestBlock._meta.db_table = 'TEST_BLOCK_T_' + '1' # #创建分表 cursor = connection.cursor() editor = BaseDatabaseSchemaEditor(connection) with BaseDatabaseSchemaEditor(connection) as editor: editor.create_model(model=TestBlock) # testBlock = TestBlock() # testBlock.BLOCK_ID = '1' # testBlock.BLOCK_NAME = 'oracle' # testBlock.save() # -------------1-end-------------------------- # -------------2-begin-------------------------- TestBlock._meta.db_table = 'TEST_BLOCK_T_' + '2' # #创建分表 cursor = connection.cursor() editor = BaseDatabaseSchemaEditor(connection) with BaseDatabaseSchemaEditor(connection) as editor: editor.create_model(model=TestBlock) # testBlock1 = TestBlock() # testBlock1.BLOCK_ID = '2' # testBlock1.BLOCK_NAME = 'mysql' # testBlock1.save() # -------------2-end-------------------------- return HttpResponse("yc is a good man")
4、view层selectTest方法 动态选择实体对应的表
def selectTest(request): TestBlock = getBlockModel('TEST_BLOCK_T_' + '1') testBlocks = TestBlock.objects.all() for testBlock in testBlocks: print('1---testBlock.BLOCK_ID:'+testBlock.BLOCK_ID) print('1---testBlock.BLOCK_NAME:'+testBlock.BLOCK_NAME) TestBlock = getBlockModel('TEST_BLOCK_T_' + '2') testBlocks = TestBlock.objects.all() for testBlock in testBlocks: print('2---testBlock.BLOCK_ID:'+testBlock.BLOCK_ID) print('2---testBlock.BLOCK_NAME:'+testBlock.BLOCK_NAME) return HttpResponse("yc is a good man")
数据结果:
1---testBlock.BLOCK_ID:1
1---testBlock.BLOCK_NAME:oracle
2---testBlock.BLOCK_ID:2
2---testBlock.BLOCK_NAME:mysql
结论是 TestBlock._meta.db_table = 'TEST_BLOCK_T_' + '1'可以动态创建表,而动态选择表只能用TestBlock = getBlockModel('TEST_BLOCK_T_' + '1') 这样的方式。
标签:TestBlock,testBlock,实体,db,动态创建,django,ID,BLOCK,NAME From: https://www.cnblogs.com/yclh/p/17598924.html