On Mon, Jan 27, 2014 at 12:52 AM, Aymeric Augustin < [email protected]> wrote:
> I wanted to write to the mailing list about this problem. Thanks for > bringing it up. > > Here's what happens. Django 1.7 is more strict about the import sequence. > Technically, the app registry population process isn't re-entrant anymore. > As a consequence, it isn't possible to call get_user_model() until the app > registry is fully populated, that is, until all app packages and models > modules have been imported. > > A milder variant of this problem existed in Django 1.6. The docs recommend > using settings.AUTH_USER_MODEL rather than get_user_model() as the target > of ForeignKeys. In fact, you could get away with get_user_model(), but you > started depending on the app registry population sequence, which wasn't > deterministic. > > The most straightforward solution is to document under which conditions > get_user_model() works. Technically, it works once all models modules have > been imported. It can't be used in models.py, but for example, it can be > used in forms.py, assuming models.py doesn't import forms.py. > > I don't know the implementation of custom user models very well. Can > someone confirm whether this restriction is acceptable, or whether it makes > get_user_model() useless? > That restriction sounds entirely acceptable to me. get_user_model() is only required when you must have a reference to a specific model (e.g., for signals or forms); models allow string references to models which are resolved as part of the model loading process. If you can use a string-form reference to the User model, the call to get_user_model() isn't required. models.py may need to *import* get_user_model(), but it doesn't need to be invoked at the module level -- all the module level usages should be able to handle the string form. Individual methods on models might call get_user_model(), but at that point, the app registry will be populated. I could even be convinced to put a safety mechanism into get_user_model() to cover this - since the app registry knows when it is fully populated, I can see a case for putting an "if not apps.is_populated(): raise Exception" in the implementation of get_user_model(). Yours, Russ Magee %-) -- You received this message because you are subscribed to the Google Groups "Django developers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/django-developers. To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CAJxq848RBE202d%3DNPSKkE8f5x2iDhqejHVfbm4B0kLyePZaSeg%40mail.gmail.com. For more options, visit https://groups.google.com/groups/opt_out.
