Custom Users + Signals (reusable app)

71 views
Skip to first unread message

Val Neekman

unread,
Oct 29, 2013, 9:42:32 PM10/29/13
to django-d...@googlegroups.com
Folks,

I am to release an app as a reusable app. However, the app makes the use of post_save signals raised by the User model and there is a problem.

With the limitation of a Custom User + Signals in mind, I cannot really call it a "true" reusable app, if it doesn't work for setups that have rolled out their own custom users.

I wonder if anyone has come across the same issue when releasing a "true" reusable app?

Thanks,

Val

charettes

unread,
Oct 29, 2013, 11:10:39 PM10/29/13
to django-d...@googlegroups.com
It is possible to attach signals to `settings.AUTH_USER_MODEL` while taking its swappable nature into account.

There might be a better way but that's how I've done it for a reusable application of mine using the `class_prepared` signal.

The technique involves looking up the model cache by app_label and model (retrieved from splitting `settings.AUTH_USER_MODEL` by '.') and falling back to `class_prepared` if it's not loaded yet. When you have a reference to the model, either through `get_model` or `class_prepared` callback, you can proceed to signal connecting.

It's a shame `class_prepared`'s sender is the model class and not a str of the `app_label.ModelName` form (just like related fields handle deferred reference). I wonder if it could be possible to change its behavior while maintaining backward compatibility by making a `model_class` an instance of a `Signal` subclass instead.

Simon

charettes

unread,
Oct 30, 2013, 12:23:30 AM10/30/13
to django-d...@googlegroups.com
Here's a POC which passes all tests on Py2 SQLite.
Reply all
Reply to author
Forward
0 new messages