Login

immitating 'real' post_syncdb signal

Author:
jango
Posted:
April 16, 2010
Language:
Python
Version:
1.1
Score:
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

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

pakalk (on April 17, 2010):
  1. Why is it 'real' signal?
  2. What is the problem with using settings.INSTALLED_APPS[-1] instead of settings.INSTALLED_APPS[last_index - 1]?
  3. Does last_index really keep last index?

#

jango (on April 18, 2010):
  1. I called it "real" because when I was looking for something like that, a bunch of people on the form keep calling this functionality a "real post_syncdb" signal because code fires after syncdb is more or less over, as opposed to when individual model is installed.

2, 3. - you are right, that wasn't too pythonish, thanks.

#

Please login first before commenting.