- April 16, 2010
- signals post_syncdb
- 0 (after 2 ratings)
I did not like the idea of having to load fixtures by creating a huge initial_data.json file. I also did not want to store my initial data in a bunch of <modelname>.sql files.
Django has post_syncdb signal which fires when model(s) for an application are installed, but I needed something that would run only once at the end of syncdb command, at least after all of the models are installed, and here's what I've come up with. The code basically checks if the current callback is for the last app in your INSTALLED_APPS, and if so, executes some fixture loading code.
1 2 3 4 5 6 7 8 9
# This code must live in management.py in one of the applications in your INSTALLED_APPS from django.db.models.signals import post_syncdb import diacre.settings as settings def load_data(sender, **kwargs): if kwargs['app'].__name__ == settings.INSTALLED_APPS[-1] + ".models": pass # load fixtures here post_syncdb.connect(load_data)
More like this
- Loading initial data per model at table creation (useful with migrations) by GigiusB 8 months, 2 weeks ago
- easy signal registration by aisbaa 3 years, 5 months ago
- Django load global fixtures test helper by vaz 3 years, 8 months ago
- Testserver: --noinput option, sending a signal by mpasternacki 4 years, 9 months ago
- Export Django data to datestamped tarball -- choose individual models for handy packaging and archiving by fish2000 4 years, 6 months ago