Django 1.5: access to user model model fields in models.py

I am working on a Django 1.5 project and I have a custom model (call CustomUser ). Another application (SomeApp) must reference this user model of the user. For the purposes of ForeignKey, etc. Django documentation says use

 User = settings.AUTH_USER_MODEL 

However, some functions in SomeApp.models must gain access to what used to be known as User.objects . But the user is now a string, not a class, so User.objects fails. An alternative would be

 from django.contrib.auth import get_user_model User = get_user_model() 

Which works in other modules, but when I use this in models.py SomeApp, Django throws an error:

Incorrectly configured ("AUTH_USER_MODEL refers to the model"% s "that was not set to"% settings.AUTH_USER_MODEL)

Any ideas?

EDIT 1 - Traceback:

 Traceback (most recent call last): File "<console>", line 1, in <module> File "...\django-badger\badger\__init__.py", line 7, in <module> from badger.models import Badge, Award, Progress File "...\django-badger\badger\models.py", line 26, in <module> User = get_user_model() File "...\lib\site-packages\django\contrib\auth\__init__.py", line 127, in get_user_model raise ImproperlyConfigured("AUTH_USER_MODEL refers to model '%s' that has not been installed" % settings.AUTH_USER_MODEL) ImproperlyConfigured: AUTH_USER_MODEL refers to model 'MyApp.AuthUser' that has not been installed 

Settings 2 - INSTALLED_APPS:

 INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.admin', 'django.contrib.admindocs', 'south', 'MyApp', # this is where my user model is defined 'SomeApp', # I try to use get_user_model() in this app models.py; doesn't work. 'social_auth', ) 
+6
source share
8 answers

Easy, I think. I had so many problems with recursive inclusions and so on ... Well, the simplest thing when you add ForeignKey is to write it like this:

 user = models.ForeignKey(settings.AUTH_USER_MODEL, null=False, on_delete=models.CASCADE, verbose_name=_(u"User")) 

If you use get_user_model , do not use it the way you do. Call

 User = get_user_model() 

at the top of the module, it will try to import your User model, which might not have been β€œinstalled”. Instead, you have several options:

  • At the top of your module write

    User = get_user_model #, then you have to use User () instead of User

  • Write get_user_model() wherever this is useful. Always in methods or functions, never being directly in the body of a model module.

+10
source

I had the same problem just now, and here are my 2 cents / solutions.

If you want to use the user model in models.py, you will use settings.AUTH_USER_MODEL for foreign keys, and for model methods you must use get_user_model() , but it must be inside the method. It will not work outside due to cyclical imports.

 from django.conf import settings from django.contrib.auth import get_user_model class Event(models.Model): recipient = models.ForeignKey(settings.AUTH_USER_MODEL) ... def get_something(self): User = get_user_model() u = User.objects.get(id=...) ... 
+8
source

Make sure your user user model is not abstract.

+3
source

I think this import from SomeApp makes circular imports. This is why in docs you can do something like ForeignKey with the call settings attribute.

For me, I came across this when using the django-filer application. In github commit, disable imports with get_user_model (). You can use the code there as an example to fix this problem.

This problem is very complicated, because when you try to call get_user_model () from the shell, it will work.

+1
source

Django documentation has the answer: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#django.contrib.auth.get_user_model

The most appropriate section: Generally speaking, you should refer to the user model with the AUTH_USER_MODEL parameter in the code that is executed during the import. get_user_model () only works after Django has imported all the models.

The real solution is to make sure that you only use get_user_model() inside the method so that it does not execute during import.

+1
source

You must install

AUTH_USER_MODEL = "yourapp.CustomUser"

in settings.py. Then get_user_model() will work. There is available documentation .

0
source

I'm starting to think that a workaround might be ok - any comments on the following in SomeApp / models.py:

 from django.contrib.auth.models import User as UserModel try: from django.contrib.auth import get_user_model except ImportError: #django <= 1.4 doesn't have get_user_model so define our own def get_user_model(): return UserModel User = getattr(settings, 'AUTH_USER_MODEL', 'auth.User') ... def SomeModel(models.Model): user = models.ForeignKey(User) # using the name of the model def some_method(self, email): qs = get_user_model().objects.filter(email=email) # using function call to get model class 
0
source

Do you use south 0.8.3?

Make sure you use a south of at least 0.8.4

GitHub issue South Release Notes

0
source

Source: https://habr.com/ru/post/948708/


All Articles