Question

I created following custom user model by using "AbstractUser" model.

from django.conf.global_settings import AUTH_USER_MODEL    

MyCustomUser(AbstractUser):
    regions = models.ManyToManyField(to='SomeModel')

    class Meta:
        app_label = 'myapp'

MyObject(models.Model):
    owner = models.ForeignKey(to=AUTH_USER_MODEL)

And I set AUTH_USER_MODEL to settings/base.py (I separated setting file each environment. e.x: settings/base.py, settings/development.py).

AUTH_USER_MODEL = 'myapp.MyCustomUser'

When I executed python manage.py syncdb, my console window has flared up by Django!

myapp.MyObject: 'owner' defines a relation with the model 'auth.User', which has been swapped out. Update the relation to point at settings.AUTH_USER_MODEL.

So, I had two questions.

  1. Is the problem is that I separated settings.py each environment.
  2. Does from django.global_settings import AUTH_USER_MODEL import myproject.settings?

And I resolved from myproject.settings.base import AUTH_USER_MODEL in this time.

Was it helpful?

Solution

global_settings is, as the name implies, the global default settings supplied by Django. So of course the default is 'auth.User'.

Since you override it in your own settings, you should import that instead. But as the documentation says, the way to import the current settings is always from django.conf import settings, rather than explicitly importing a settings file in your project.

Also note though that rather than using the settings at all here - which could lead to some dependency issues on startup - you should be using get_user_model() from django.contrib.auth, as explained in the documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top