Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#19218 closed Bug (fixed)

Can't use get_user_model() when connect to signal sender

Reported by: kunitoki@… 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 anonymous, 12 years ago

Resolution: invalid
Status: newclosed

Sorry, was a mistake i made...

comment:2 by anonymous, 12 years ago

Resolution: invalid
Status: closedreopened
Type: UncategorizedBug

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 Russell Keith-Magee, 12 years ago

Resolution: invalid
Status: reopenedclosed

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 anonymous, 12 years ago

Resolution: invalid
Status: closedreopened

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 anonymous, 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 Russell Keith-Magee, 12 years ago

Severity: NormalRelease blocker
Triage Stage: UnreviewedAccepted

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 anonymous, 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 Russell Keith-Magee <russell@…>, 12 years ago

Resolution: fixed
Status: reopenedclosed

In fdb5c98d7ee54c7f89ec10b0203263f1f5b37510:

Fixed #19218 -- Added documentation note on limitations of signals with custom User models.

Thanks to kunitoki@… for the report.

comment:9 by Russell Keith-Magee <russell@…>, 12 years ago

In 24582f18ff3aeb263f951dd2acf56f0a857fda17:

[1.5.x] Fixed #19218 -- Added documentation note on limitations of signals with custom User models.

Thanks to kunitoki@… for the report.

Backport of fdb5c98d7ee54c7f89ec10b0203263f1f5b37510.

comment:10 by kunitoki@…, 11 years ago

Thanx Russel for the fix !

Note: See TracTickets for help on using tickets.
Back to Top