28👍
After some digging around I found this
The culprit is a function clean_username
inside UserCreationForm
inside django.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
def clean_username(self):
# Since User.username is unique, this check is redundant,
# but it sets a nicer error message than the ORM. See #13147.
username = self.cleaned_data["username"]
try:
User._default_manager.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(self.error_messages['duplicate_username'])
The User
in this file is directly referencing to the builtin user model.
To fix it, I created my custom forms
from models import User #you can use get_user_model
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth import forms
class MyUserCreationForm(UserCreationForm):
def clean_username(self):
# Since User.username is unique, this check is redundant,
# but it sets a nicer error message than the ORM. See #13147.
username = self.cleaned_data["username"]
try:
User._default_manager.get(username=username)
except User.DoesNotExist:
return username
raise forms.ValidationError(self.error_messages['duplicate_username'])
class Meta(UserCreationForm.Meta):
model = User
class MyUserAdmin(UserAdmin):
add_form = MyUserCreationForm
admin.site.register(User,MyUserAdmin)
Or you can try monkey patching the original UserCreationForm
to replace the User
variable.
12👍
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.
$ ./manage.py makemigrations <my_app>
$ ./manage.py migrate
- Access token from view
- How to delete nested session data in Django?
- Django Gunicorn not load static files
- ValidationError while using ModelForm Django
- How to convert a list of dictionaries to JSON in Python / Django?
5👍
This is due to migration is not run. This issue is resolved for me by running following command:
python manage.py syncdb
- Ajax, CSRF and DELETE
- Tracking changes to all models in Django
- Django sub-applications & module structure
- Django-compressor: how to write to S3, read from CloudFront?
3👍
Migrate your app (the one with custom user model) first, and only then the rest:
$ ./manage.py makemigrations <your_app>
$ ./manage.py migrate
$ ./manage.py makemigrations
$ ./manage.py migrate
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
- 'QuerySet' object has no attribute ERROR, trying to get related data on ManyToMany fields
- Django: Tweaking @login_required decorator
0👍
I had to:
- delete the database
- create a new database
- delete all migrations from all models
- make migrations anew
- migrate
It was double annoying because I had to do it on local and remote server. I tried just deleting migrations or deleting the database but then git would push/pull the old migrations and messed everything up.
P.S. This bug initially arose because I ran the migrations before adding AUTH_USER_MODEL = 'customauth.MyUser'
to the settings.py
file, which is not version controlled.
- Django: IntegrityError during Many To Many add()
- Create multiple objects without multiple hits to the db in Django 1.8
0👍
You haven’t created the admin in the database. If you are using an up to date version of Django
Try:
python manage.py makemigrations
python manage.py migrate
Am a bit curious about how you can have a login and password if the auth tables don’t exist?
The last command saved me a lot of time
- Why does Django South 1.0 use iteritems()?
- 'QuerySet' object has no attribute ERROR, trying to get related data on ManyToMany fields
- Is it ok to catch and reraise an exception inside Django transaction.atomic()?
- How do I call a model method in django ModelAdmin fieldsets?