3

如何Dynamic models在 Django中创建1.8

根据官方网站,我做了以下事情:

>>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'

如何解决?

4

0 回答 0