在Django 1.8中创建动态模型

时间:2015-12-03 06:16:24

标签: python django django-models django-modeladmin django-shell

如何在Django中创建Dynamic models 1.8

我根据official site完成了以下内容:

>>attrs = {
            'name': models.CharField(max_length=32),
            '__module__': 'myapp.models'
          }
>>person = type("ptable", (models.Model,), attrs)

>>def install(model):
      from django.core.management import sql, color
      from django.db import connection
      style = color.no_style()
      cursor = connection.cursor()
      statements, pending = sql.sql_model_create(model, style)
      for sql in statements:
          cursor.execute(sql)

>>install(person)

但它给了我这个错误:

Traceback (most recent call last):
File "<console>", line 1, in <module>
File "<console>", line 6, in install
AttributeError: 'module' object has no attribute 'sql_model_create'

如何解决?

0 个答案:

没有答案
相关问题