开发者_Go百科I'm wondering if there is a way to run a custom method when syncdb is executed. What I am trying to accomplish is to load a mail template into the database. As I need nearly the same template in multiple entries in the db, I don't want to place it in the initial_data file.
I've searched the django docs and the web, but couldn't find any solution for this.
Any help is appreciated.
How about the post_syncdb
signal?
http://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.post_syncdb
Connect a handler to the signal and run whatever you wish in your function.
Well, it might work in your case, it might not. post_syncdb
is called after table creation and sent to all apps, so if you want one method that handles all default data creation, you'd have to use something like the following snippet to detect if the signal is for the last app in the list to ensure your tables exist :P.
http://djangosnippets.org/snippets/1988/
Well, it's an idea. Enjoy!
精彩评论