3

How to create Dynamic models in Django 1.8?

I have done the following according to the 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)

But it gives me this error:

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'

How to solve it?

cold_coder
  • 584
  • 4
  • 8
  • 24

0 Answers0