模型自动创建
来源:互联网 发布:南京凶宅数据库 编辑:程序博客网 时间:2024/06/05 21:22
# -*- coding: utf-8 -*-
from django.contrib import admin
from django.db import models
def create_model(model_name, app_label='', fields=None, module='', meta_options=None, admin_options=None):
"""
Create specified model
"""
class Meta:
# Using type('Meta', ...) gives a dictproxy error during model creation
pass
if app_label:
# app_label must be set using the Meta inner class
setattr(Meta, 'app_label', app_label)
# Update Meta with any options that were provided
if meta_options is not None:
for key, value in meta_options.iteritems():
setattr(Meta, key, value)
# Set up a dictionary to simulate declarations within a class
attrs = {'__module__': module, 'Meta': Meta}
# Add in any fields that were provided
if fields:
attrs.update(fields)
# Create the class, which automatically triggers ModelBase processing
model = type(model_name, (models.Model,), attrs)
# Create an Admin class if admin options were provided
if admin_options is not None:
class Admin(admin.ModelAdmin):
pass
for key, value in admin_options.items():
setattr(Admin, key, value)
admin.site.register(model, Admin)
return model
from django.contrib import admin
from django.db import models
def create_model(model_name, app_label='', fields=None, module='', meta_options=None, admin_options=None):
"""
Create specified model
"""
class Meta:
# Using type('Meta', ...) gives a dictproxy error during model creation
pass
if app_label:
# app_label must be set using the Meta inner class
setattr(Meta, 'app_label', app_label)
# Update Meta with any options that were provided
if meta_options is not None:
for key, value in meta_options.iteritems():
setattr(Meta, key, value)
# Set up a dictionary to simulate declarations within a class
attrs = {'__module__': module, 'Meta': Meta}
# Add in any fields that were provided
if fields:
attrs.update(fields)
# Create the class, which automatically triggers ModelBase processing
model = type(model_name, (models.Model,), attrs)
# Create an Admin class if admin options were provided
if admin_options is not None:
class Admin(admin.ModelAdmin):
pass
for key, value in admin_options.items():
setattr(Admin, key, value)
admin.site.register(model, Admin)
return model
0 0
- 模型自动创建
- 领域模型。hibernate自动创建表????
- 使用代码自动创建模型属性
- 一个小工具根据json字符串自动创建模型类
- Django创建应用、模型、配置后台自动管理
- 创建模型
- 自动创建
- 自动测试的模型
- 模型自动居中
- 自动字典转模型
- Thinkphp 模型->自动验证
- Thinkphp 模型->自动完成
- 创建对象模型
- Nhibernate创建模型
- poverDesiger创建模型小结
- Anylogic模型创建
- astah创建模型
- (43)创建模型对象
- netCDF4-python的安装
- 1000!输出指定位
- 动态规划总结
- 防阻止打开新标签页加载页面方案本人试过可用
- JavaScript中逻辑运算符(&&、||、!)的优先级和运算顺序
- 模型自动创建
- 土豆肉丝做法
- Qt 获取Windows内存信息
- 解决VMware下64位Linux系统不支持全虚拟化(VMX)
- android--fragment的使用
- Web容器启动时加载Spring分析
- 安卓获取版本号
- Android SDK Manager目录解析
- c#面向对象特征(2)之多态