""" usage: python creates.py [MODEL_1] [MODEL_2] ... [MODEL_N] Prints CREATE TABLE statements for all models in the models.py file found in the same directory as this script. If extra command line arguments are provided, it only prints the CREATE TABLE statements for those models. """ import models from sys import argv from django.core.management.sql import sql_model_create from django.db.models import Model from django.db.models.base import ModelBase classes = [thing for thing in models.__dict__.values() if type(thing) is ModelBase] class Style: def __getattr__(self, name): return lambda text: text classes.remove(Model) if len(argv)>1: classes = [c for c in classes if c.__name__ in argv[1:]] print("") for model in classes: print("DROP TABLE IF EXISTS _"+model.__name__.lower()+";") print( sql_model_create(model, Style(), classes)[0][0] ) print("")