4

I am building a django web app with a custom user model which extends the AbstractBaseUser. In one of my models I need to use this custom user model:

from accounts.models import User

class History(models.Model):
    user = models.ForeignKey(User)
    job = models.ForeignKey(Job)

When I try to run the python manage.py makemigrations command this error message is outputted:

ImportError: cannot import name User

In my settings.py I do the following to let django know that there is a custom user model:

AUTH_USER_MODEL = "accounts.User"

I am puzzled as to what I am doing wrong. Surely there must be a way to import this model that I do not know of. How do I fix this?

I have tried to use the get_user_model() method, however it doesn't work in the model as the models haven't loaded yet. This is therefore not a solution. Any other ideas?

Thank you in advance.

Tom Finet
  • 2,056
  • 6
  • 30
  • 54

1 Answers1

6

You can do as follows:

from django.conf import settings
class History(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL)
    job = models.ForeignKey(Job)

Please refer to documentation for more details.

If you reference User directly (for example, by referring to it in a foreign key), your code will not work in projects where the AUTH_USER_MODEL setting has been changed to a different user model.

get_user_model()[source]

Instead of referring to User directly, you should reference the user model using django.contrib.auth.get_user_model(). This method will return the currently active user model – the custom user model if one is specified, or User otherwise.

When you define a foreign key or many-to-many relations to the user model, you should specify the custom model using the AUTH_USER_MODEL setting. For example

Community
  • 1
  • 1
Gagik Sukiasyan
  • 846
  • 6
  • 17
  • 1
    The docs say to do what I have done: `from myapp.models import CustomUser` – Tom Finet Mar 20 '17 at 19:15
  • The place it says is when it describes custom user models in admin interface. Please click to documentation link I gave in the answer and see what it says. Will copy/paste some notes to answer as well shortly – Gagik Sukiasyan Mar 20 '17 at 19:18
  • I have changed the import statement to: `from django.contrib.auth import get_user_model` but there is an error which is: `django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.` @Gagik Sukiasyan – Tom Finet Mar 20 '17 at 19:52
  • Did you add you accounts app into INSTALLED_APPS ? – Gagik Sukiasyan Mar 20 '17 at 19:53
  • `INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'bootstrap3', 'accounts', 'jobs', ]` – Tom Finet Mar 20 '17 at 19:54
  • move the accounts to be before the django.contrib.auth, otherwise it will not see, I guess – Gagik Sukiasyan Mar 20 '17 at 19:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/138567/discussion-between-gagik-sukiasyan-and-tom-finet). – Gagik Sukiasyan Mar 20 '17 at 20:02
  • Using the get_user_model() method doesn't work in the model as the models haven't loaded yet. This is therefore not a solution. Any other ideas? @Gagik Sukiasyan – Tom Finet Mar 21 '17 at 13:31
  • I didn't try writing code and testing and I always used other way of extending the user, by creating other model by having one to one relation to auth user. But here I think the right solution is to use the settings.AUTH_USER_MODEL as I provided in my answer. Do you mean it also didn't work, or you only tried the get_user_model ? – Gagik Sukiasyan Mar 21 '17 at 17:46
  • No I have tried the settings.AUTH_USER_MODEL method and it has worked. Thanks – Tom Finet Mar 21 '17 at 17:48