signals - Django app post_save with sender=get_user_model() -


i have django app needs receive post_save signal when user model saved, without knowing main project directly. here's basic layout:

project/     __init__.py     settings.py     models.py app/     __init__.py     models.py     signals.py 

it must work django 1.6, can't use ready() method. in app/__init__.py have:

# app/__init__.py  import signals 

and in app/signals.py:

# app/signals.py  django.contrib.auth import get_user_model django.db.models.signals import post_save django.dispatch import receiver  @receiver(post_save, sender=get_user_model()) def user_save_callback(sender, instance, **kwargs):     ... 

the installed apps in project/settings include:

# project/settings.py  installed_apps = (     ...     'django.contrib.auth',     'project',     'app',     ... 

the custom user model defined in project/models.py. signals module being loaded correct user model (a quick print statement @ top of signals.py serves demonstrate this).

however, callback function never gets called when instance of user model saved. django-debug-toolbar helped me verify receivers not being registered. gives? thoughts might have appreciated ;)

----- edit -----

it ended being silly problem, i'm not posting answer. it's large project , there signal receiver somewhere else same name. other 1 getting defined after mine, overwriting in registry of receivers :p

i guess it's lesson in naming conventions... if you're going register signal handler, name specific. if there multiple handlers same signal.

i updated project 1.7 1.8 , ran issue.

i replacing old 'auth.user'and django.contrib.auth.models.userreferences , stumbled upon following error:

django.core.exceptions.appregistrynotready: models aren't loaded yet. 

reading through traceback found out did following in models.py:

models.signals.post_save.connect(create_user_profile, sender=get_user_model()) 

this second time got issue, following might solution others. replace get_user_modelfunction string or setting:

models.signals.post_save.connect(create_user_profile, sender=settings.auth_user_model) 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -