Login

Post-post-syncdb and post-post-migrate

Author:
theY4Kman
Posted:
April 16, 2015
Language:
Python
Version:
1.6
Score:
0 (after 0 ratings)

The post_syncdb (Django pre-1.7) and post_migrate (>=1.7 and South) signals are fired for every single app. What I really wanted was one signal fired after the migration or syncdb completed. There's no official one, and all the snippets were doing horribly hacky things (and wouldn't work for South, anyway). This one will work for Django syncdb, and South migrate.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
@receiver(post_migrate)
@receiver(post_syncdb)
def post_post_syncdb(sender=None, verbosity=0, **kwargs):
    from django.db.models.loading import cache

    # post-syncdb and -migrate are run for *each app*. This snippet determines
    # whether we're running after the LAST app in the queue.

    models_module = kwargs['app']
    if isinstance(models_module, basestring):
        # post_syncdb returns the models module, but post_migrate an app label
        models_module = cache.get_app(models_module)

    last_models_module = cache.app_store.keyOrder[-1]
    if models_module != last_models_module:
        return

    # Put your code here.

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 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, 7 months ago

Comments

ZaherSarieddine (on June 4, 2015):

It did not work as is with django 1.8 (complained about 'app') I tried then another simpler mechanism which seems to be working. It checks for a certain app name so that it performs the code only once:

def post_post_migrate(sender=None, verbosity=0, **kwargs): if sender.name != 'myapps.administration': return

# Put your code here.

I printed all the names first and chose the last one in the list.

#

Please login first before commenting.