Last modified 3 years ago
db.create_table
db.create_table(table_name, fields) fields = ((field_name, models.SomeField(somearg=4)), ...)
This call creates a table called table_name in the database with the schema specified by fields, which is a tuple of (field_name, field_instance) tuples.
Note that this call will not automatically add an id column; you are responsible for doing that.
We recommend you create calls to this function using ./manage.py startmigration appname migrationname --model Foo, which will create a migration that makes the model Foo. You are allowed to pass multiple --model arguments.
Examples
A simple table, with one field, name, and the default id column:
db.create_table('core_planet', ( ('id', models.AutoField(primary_key=True)), ('name', models.CharField(unique=True, max_length=50)), ))
A more complex table, which uses the ORM Freezer for its foreign keys:
db.create_table('core_nation', ( ('name', models.CharField(max_length=255)), ('short_name', models.CharField(max_length=50)), ('slug', models.SlugField(unique=True)), ('planet', models.ForeignKey(orm.Planet, related_name="nations")), ('flag', models.ForeignKey(orm.Flag, related_name="nations")), ('planet_name', models.CharField(max_length=50)), ('id', models.AutoField(primary_key=True)), ))
