Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot

TL;DR: Use the code from the Solution part at the end of the following answer.

Longer explanation: You see, as of Django 1.5, it's not enough to subclass Django's UserAdmin to be able to interact with swappable user models: you need to override respective forms as well.

If you jump to django.contrib.auth.admin source, you'll see that the UserAdmin's form and add_form have these values:

# django/contrib/auth/admin.py class UserAdmin(admin.ModelAdmin): ... form = UserChangeForm add_form = UserCreationForm 

Which point us to forms in django.contrib.auth.forms that do not respect swappable user models:

# django/contrib/auth/forms.py class UserCreationForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. class UserChangeForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. 

Solution: So, you should follow a great already existing answeranswer (don't forget to vote it up!) which boils down to this:

from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm class MyUserChangeForm(UserChangeForm): class Meta: model = get_user_model() class MyUserCreationForm(UserCreationForm): class Meta: model = get_user_model() class MyUserAdmin(UserAdmin): form = MyUserChangeForm add_form = MyUserCreationForm admin.site.register(MyUser, MyUserAdmin) 

Hopefully, this would be fixed in the future releases of Django (here's the corresponding ticket in the bug tracker).

TL;DR: Use the code from the Solution part at the end of the following answer.

Longer explanation: You see, as of Django 1.5, it's not enough to subclass Django's UserAdmin to be able to interact with swappable user models: you need to override respective forms as well.

If you jump to django.contrib.auth.admin source, you'll see that the UserAdmin's form and add_form have these values:

# django/contrib/auth/admin.py class UserAdmin(admin.ModelAdmin): ... form = UserChangeForm add_form = UserCreationForm 

Which point us to forms in django.contrib.auth.forms that do not respect swappable user models:

# django/contrib/auth/forms.py class UserCreationForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. class UserChangeForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. 

Solution: So, you should follow a great already existing answer (don't forget to vote it up!) which boils down to this:

from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm class MyUserChangeForm(UserChangeForm): class Meta: model = get_user_model() class MyUserCreationForm(UserCreationForm): class Meta: model = get_user_model() class MyUserAdmin(UserAdmin): form = MyUserChangeForm add_form = MyUserCreationForm admin.site.register(MyUser, MyUserAdmin) 

Hopefully, this would be fixed in the future releases of Django (here's the corresponding ticket in the bug tracker).

TL;DR: Use the code from the Solution part at the end of the following answer.

Longer explanation: You see, as of Django 1.5, it's not enough to subclass Django's UserAdmin to be able to interact with swappable user models: you need to override respective forms as well.

If you jump to django.contrib.auth.admin source, you'll see that the UserAdmin's form and add_form have these values:

# django/contrib/auth/admin.py class UserAdmin(admin.ModelAdmin): ... form = UserChangeForm add_form = UserCreationForm 

Which point us to forms in django.contrib.auth.forms that do not respect swappable user models:

# django/contrib/auth/forms.py class UserCreationForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. class UserChangeForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. 

Solution: So, you should follow a great already existing answer (don't forget to vote it up!) which boils down to this:

from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm class MyUserChangeForm(UserChangeForm): class Meta: model = get_user_model() class MyUserCreationForm(UserCreationForm): class Meta: model = get_user_model() class MyUserAdmin(UserAdmin): form = MyUserChangeForm add_form = MyUserCreationForm admin.site.register(MyUser, MyUserAdmin) 

Hopefully, this would be fixed in the future releases of Django (here's the corresponding ticket in the bug tracker).

Fixed few typos.
Source Link
Ivan Kharlamov
  • 2k
  • 2
  • 24
  • 33

TL;DR: FollowUse the code from the Solution part at the end of thisthe following answer.

Longer explanation: You see, as of Django 1.5, it's not enough to subclass Django's UserAdmin to be able to interact with swappable user models: you need to override respective forms as well.

If you jump to django.contrib.auth.admin source, youyou'll see that the UserAdmin's form and add formadd_form have these values:

# django/contrib/auth/admin.py class UserAdmin(admin.ModelAdmin): ... form = UserChangeForm add_form = UserCreationForm 

Which point us to forms in django.contrib.auth.forms that do not respect swappable user models:

# django/contrib/auth/forms.py class UserCreationForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. class UserChangeForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. 

Solution: So, you should follow ana great already existing great answeranswer (don't forget to vodevote it up!) which boils down to this:

from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm class MyUserChangeForm(UserChangeForm): class Meta: model = get_user_model() class MyUserCreationForm(UserCreationForm): class Meta: model = get_user_model() class MyUserAdmin(UserAdmin): form = MyUserChangeForm add_form = MyUserCreationForm admin.site.register(MyUser, MyUserAdmin) 

Hopefully, this would be fixed in the future releases of Django (here's the corresponding ticket in the bug tracker).

TL;DR: Follow the Solution part at the end of this answer.

Longer explanation: You see, as of Django 1.5, it's not enough to subclass Django's UserAdmin to be able to interact with swappable user models: you need to override respective forms as well.

If you jump to django.contrib.auth.admin source, you see that the UserAdmin's form and add form have these values:

# django/contrib/auth/admin.py class UserAdmin(admin.ModelAdmin): ... form = UserChangeForm add_form = UserCreationForm 

Which point us to forms in django.contrib.auth.forms that do not respect swappable user models:

# django/contrib/auth/forms.py class UserCreationForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. class UserChangeForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. 

Solution: So, you should follow an already existing great answer (don't forget to vode it up!) which boils down to this:

from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm class MyUserChangeForm(UserChangeForm): class Meta: model = get_user_model() class MyUserCreationForm(UserCreationForm): class Meta: model = get_user_model() class MyUserAdmin(UserAdmin): form = MyUserChangeForm add_form = MyUserCreationForm admin.site.register(MyUser, MyUserAdmin) 

Hopefully, this would be fixed in the future releases of Django (here's the corresponding ticket in the bug tracker).

TL;DR: Use the code from the Solution part at the end of the following answer.

Longer explanation: You see, as of Django 1.5, it's not enough to subclass Django's UserAdmin to be able to interact with swappable user models: you need to override respective forms as well.

If you jump to django.contrib.auth.admin source, you'll see that the UserAdmin's form and add_form have these values:

# django/contrib/auth/admin.py class UserAdmin(admin.ModelAdmin): ... form = UserChangeForm add_form = UserCreationForm 

Which point us to forms in django.contrib.auth.forms that do not respect swappable user models:

# django/contrib/auth/forms.py class UserCreationForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. class UserChangeForm(forms.ModelForm): ... class Meta: model = User # non-swappable User model here. 

Solution: So, you should follow a great already existing answer (don't forget to vote it up!) which boils down to this:

from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm class MyUserChangeForm(UserChangeForm): class Meta: model = get_user_model() class MyUserCreationForm(UserCreationForm): class Meta: model = get_user_model() class MyUserAdmin(UserAdmin): form = MyUserChangeForm add_form = MyUserCreationForm admin.site.register(MyUser, MyUserAdmin) 

Hopefully, this would be fixed in the future releases of Django (here's the corresponding ticket in the bug tracker).

deleted 437 characters in body
Source Link
Ivan Kharlamov
  • 2k
  • 2
  • 24
  • 33

TL;DR: You should followFollow the this adviseSolution part at the end of this answer.

Longer explanation: You see, as of Django 1.5Django 1.5, it's not enough to subclass Django's UserAdmin, to be able to interact with swappable user models: you need to override respective forms as well.

If you jump to django.contrib.auth.admin sourcedjango.contrib.auth.admin source, you see that the UserAdmin's form and add form have these values:

Which point us to formsforms in django.contrib.auth.forms that do not respect swappable user models:

Solution: So, you should follow andan already existing great answer (don't forget to vode it up!) which boils down to this:

from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm   class MyUserChangeForm(UserChangeForm):   class Meta: model = get_user_model() class MyUserCreationForm(UserCreationForm):   class Meta: model = get_user_model()  def clean_username(self): username = self.cleaned_data["username"] try: get_user_model().objects.get(username=username) except get_user_model().DoesNotExist: return username raise forms.ValidationError( self.error_messages['duplicate_username'] ) class MyUserAdmin(UserAdmin): form = MyUserChangeForm add_form = MyUserCreationForm fieldsets = ( (None, {'fields': [('username', 'password'),]}),  (_('Personal info'),  {'fields': ('first_name', 'last_name', 'email')}), (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}), (_('Important dates'), {'fields': ('last_login', 'date_joined')}), ) admin.site.register(MyUser, MyUserAdmin) # Add important stuff here admin.site.register(MyUser, MyUserAdmin) 

Hopefully, this would be fixed in the future releases of Django (here's the corresponding ticket in the bug tracker).

TL;DR: You should follow this advise.

Longer explanation: You see, as of Django 1.5, it's not enough to subclass Django's UserAdmin, you need to override respective forms as well.

If you jump to django.contrib.auth.admin source, you see that the UserAdmin's form and add form have these values:

Which point us to forms that do not respect swappable user models:

So, you should follow and already existing great answer (don't forget to vode it up!):

from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm   class MyUserChangeForm(UserChangeForm):   class Meta: model = get_user_model() class MyUserCreationForm(UserCreationForm):   class Meta: model = get_user_model()  def clean_username(self): username = self.cleaned_data["username"] try: get_user_model().objects.get(username=username) except get_user_model().DoesNotExist: return username raise forms.ValidationError( self.error_messages['duplicate_username'] ) class MyUserAdmin(UserAdmin): form = MyUserChangeForm add_form = MyUserCreationForm fieldsets = ( (None, {'fields': [('username', 'password'),]}),  (_('Personal info'),  {'fields': ('first_name', 'last_name', 'email')}), (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions')}), (_('Important dates'), {'fields': ('last_login', 'date_joined')}), ) admin.site.register(MyUser, MyUserAdmin) # Add important stuff here admin.site.register(MyUser, MyUserAdmin) 

TL;DR: Follow the Solution part at the end of this answer.

Longer explanation: You see, as of Django 1.5, it's not enough to subclass Django's UserAdmin to be able to interact with swappable user models: you need to override respective forms as well.

If you jump to django.contrib.auth.admin source, you see that the UserAdmin's form and add form have these values:

Which point us to forms in django.contrib.auth.forms that do not respect swappable user models:

Solution: So, you should follow an already existing great answer (don't forget to vode it up!) which boils down to this:

from django.contrib.auth import get_user_model from django.contrib.auth.admin import UserAdmin from django.contrib.auth.forms import UserCreationForm, UserChangeForm class MyUserChangeForm(UserChangeForm): class Meta: model = get_user_model() class MyUserCreationForm(UserCreationForm): class Meta: model = get_user_model() class MyUserAdmin(UserAdmin): form = MyUserChangeForm add_form = MyUserCreationForm admin.site.register(MyUser, MyUserAdmin) 

Hopefully, this would be fixed in the future releases of Django (here's the corresponding ticket in the bug tracker).

Source Link
Ivan Kharlamov
  • 2k
  • 2
  • 24
  • 33
Loading