Is there a way to run some custom SQL statements after syncdb does it thing creating the tables for the models? Specifically, I would like to create some database views.
2 Answers
Yes, there are signals you can catch after a syncdb.
See management signals for docs.
This is how the contrib.auth permissions table is populated, as well as the contenttypes framework table.
Comments
Note: As mentioned in the comments, this method is deprecated as of Django 1.7.
Or just create a file called sql/<modelname>.sql: http://docs.djangoproject.com/en/dev/howto/initial-data/#providing-initial-sql-data
4 Comments
Carl Meyer
This works nicely enough, but the entire "custom SQL" feature has been termed a "hack" by one of the core developers, and a preference was expressed for using the post_syncdb signal. Can't find link at the moment. In any case it's guaranteed to stick around until 2.0, so no worries.
Cerin
This does not work. Running
manage.py sqlcustom <myapp> ignores all my sql/<modelname>.sql files.Dave
Hmm. I haven't tried this in quite a while, but the latest docs still indicate this should work. The files should be called <appname>/sql/<modelname>.sql
David
Deprecated officially as of Django 1.7 docs.djangoproject.com/en/1.7/howto/initial-data/…