Skip to content

Instantly share code, notes, and snippets.

@themartorana
Created February 16, 2011 19:40
Show Gist options
  • Save themartorana/830010 to your computer and use it in GitHub Desktop.
Save themartorana/830010 to your computer and use it in GitHub Desktop.
#
#
def create_table_for_unrelated_model(model, database_name):
from django.db import connections, transaction
from django.core.management.sql import custom_sql_for_model
from django.core.management.color import no_style
connection = connections[database_name]
cursor = connection.cursor()
if not model._meta.managed:
model._meta.managed = True
sql, references = connection.creation.sql_create_model(model, no_style())
if sql and len(sql) > 0:
sql_to_execute = sql[0]
cursor.execute(sql_to_execute)
transaction.commit_unless_managed(using=database_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment