目的:在数据库中动态添加表 环境:django-1.2, mysql-5.1.47 代码: dynamic_model.py # -*- coding: utf-8 -*- from django.db import models def create_model(name, fields=None, app_label='', module='', options=None, admin=None): 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 options is not None: for key, value in options.items(): 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: