6

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 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

4 Comments

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.
This does not work. Running manage.py sqlcustom <myapp> ignores all my sql/<modelname>.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 <appname>/sql/<modelname>.sql
Deprecated officially as of Django 1.7 docs.djangoproject.com/en/1.7/howto/initial-data/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.