#19218 closed Bug (fixed)
Can't use get_user_model() when connect to signal sender
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | contrib.auth | Version: | 1.5-alpha-1 |
Severity: | Release blocker | Keywords: | auth user get_user_model signals connect receiver |
Cc: | Triage Stage: | Accepted | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I've tried doing this in django 1.5 alpha1:
settings.py:
AUTH_USER_MODEL = 'myapp.MyUser'
models.py:
@receiver(user_logged_in, sender=myapp.MyUser, dispatch_uid='working_func_1') def working_func(sender, request, user, **kwargs): print "this will execute" @receiver(user_logged_in, sender=get_user_model(), dispatch_uid='not_working_func_2') def not_working_func(sender, request, user, **kwargs): print "this will not work"
Any ideas ? New custom user model is great, but should work out of the box for things like these !
Change History (10)
comment:1 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 12 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
Type: | Uncategorized → Bug |
Now if i have a signal that register after i have defined a new AUTH_USER_MODEL:
base/models.py:
class BaseUser(AbstractUser): pass @receiver(user_logged_in, sender=get_user_model())
Now i get:
ImproperlyConfigured: AUTH_USER_MODEL refers to model 'base.BaseUser' that has not been installed
comment:3 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | reopened → closed |
This suggests to me that you haven't put the 'base' app in your INSTALLED_APPS. Closing invalid; feel free to reopen if you can verify that your settings file is correct.
comment:4 by , 12 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
My settings file is correct, the problem is that you can't call get_user_model() at global scope in the same models.py that define your AUTH_USER_MODEL (especially when assigning to the sender argument in signal registration).
Probably this is normal python precedence behaviour (get_user_model try to access a model that isn't registered to django models as the current module has not finished parsing), or eventually get_user_model could be lazy instead...
comment:5 by , 12 years ago
My settings file is correct, the problem is that you can't call get_user_model() at global scope in the same models.py that define your AUTH_USER_MODEL (especially when assigning to the sender argument in signal registration).
Probably this is normal python precedence behaviour (get_user_model try to access a model that isn't registered to django models as the current module has not finished parsing), or eventually get_user_model could be lazy instead...
comment:6 by , 12 years ago
Severity: | Normal → Release blocker |
---|---|
Triage Stage: | Unreviewed → Accepted |
Ok - I see the problem now.
Marking as a release blocker because it's a problem with a new feature. However, my gut tells me that this is a case where we just need to document the limitations on get_user_model(). Essentially, we're in a situation where we need to defer installation of the signal handler until after the models have been loaded. That means that either you can't install that signal in the models.py file (urls.py might be a viable alternative), or you need to specifically reference the model you want to attach the signal to.
The long term fix is to find a better way to register signals, which is what #3591 will allow.
comment:7 by , 12 years ago
Yes, i think signal handlers should be registered after all models have been installed (i will try in urls, but it doesn't seem a logical place where to do it imho).
Anyway this is not a showstopper for me now (instead of using get_user_model() i specify the my subclassed user class directly even if this is not clean, making the whole point of get_user_model rather useless.
comment:8 by , 12 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
Sorry, was a mistake i made...