Previously in Django 1.6 and earlier versions, I used to do the following to make User
's email attribute unique:
class User(AbstractUser):
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = ['username']
User._meta.get_field_by_name('email')[0]._unique=True
I'm migrating to Django 1.7 but this code is raising the following error:
django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.
traced all the way back to User._meta.get_field_by_name('email')[0]._unique=True
.
How should I migrate this to Django 1.7?
According to the documentation,
ready()
method ofAppConfig
is called when the registry is populated which means models are also loaded, therefore referencing models shouldn't be a problem.That line of code is still not valid as it is in
ready()
though, as pointed out in the documentation:Therefore, remove
User._meta.get_field_by_name('email')[0]._unique=True
frommodels.py
and do the following in your app configuration instead: