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.
Asked
Active
Viewed 2,517 times
2 Answers
9
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.

Van Gale
- 43,536
- 9
- 71
- 81
4
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

Dave
- 3,171
- 1
- 25
- 23
-
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. – Carl Meyer Mar 05 '09 at 16:16
-
This does not work. Running `manage.py sqlcustom
` ignores all my sql/ – Cerin May 13 '13 at 00:19.sql files. -
Hmm. I haven't tried this in quite a while, but the latest docs still indicate this should work. The files should be called
/sql/ – Dave May 16 '13 at 16:47.sql -
Deprecated officially as of Django 1.7 https://docs.djangoproject.com/en/1.7/howto/initial-data/#providing-initial-sql-data – David Oct 23 '14 at 02:43