I have a custom user model as below:
class User(AbstractUser):
subscribe_newsletters = models.BooleanField(default=True)
old_id = models.IntegerField(null=True, blank=True)
old_source = models.CharField(max_length=25, null=True, blank=True)
And using the builtin UserAdmin
admin.site.register(User, UserAdmin)
While editing the user record works fine, but when I add a user, I get the following error
Exception Value:
relation "auth_user" does not exist
LINE 1: ...user"."is_active", "auth_user"."date_joined" FROM "auth_user...
After some digging around I found this
https://docs.djangoproject.com/en/1.5/topics/auth/customizing/#custom-users-and-the-built-in-auth-forms
The culprit is a function
clean_username
insideUserCreationForm
insidedjango.contrib.auth.forms.py
. A few tickets have been created, but apparently the maintainers don't think it's a defect:https://code.djangoproject.com/ticket/20188
https://code.djangoproject.com/ticket/20086
The
User
in this file is directly referencing to the builtin user model.To fix it, I created my custom forms
Or you can try monkey patching the original
UserCreationForm
to replace theUser
variable.This is due to migration is not run. This issue is resolved for me by running following command:
python manage.py syncdb
Migrate your app (the one with custom user model) first, and only then the rest:
You can also control the order of migrations to make sure this happens automatically, see https://docs.djangoproject.com/en/1.10/howto/writing-migrations/#controlling-the-order-of-migrations
Django 1.8
If your app is not yet using migrations then this could also be the problem, as contrib.auth uses them. Enabling migrations for my app solved it for me.