Opened 10 years ago

Closed 10 years ago

#21616 closed Bug (fixed)

Manager isn't available; User has been swapped for 'myuser.MyUser'

Reported by: charlesw1234@… Owned by: nobody
Component: Uncategorized Version: 1.6
Severity: Normal Keywords: swapped for
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I just want to do something more when the password is set for user.

So the following line is added to settings.py:

AUTH_USER_MODEL='myuser.MyUser'

Here is myuser/models.py:

from django.db import models
from django.contrib.auth.models import AbstractUser

# Create your models here.
class MyUser(AbstractUser):
    def set_password(self, raw_password):
        AbstractUser.set_password(raw_password)
        print('raw_password =', raw_password)

And myuser/admin.py:

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import User
from myuser.models import MyUser

# Register your models here.

class MyUserCreationForm(UserCreationForm):
    class Meta:
        model = MyUser

class MyUserChangeForm(UserChangeForm):
    class Meta:
        model = MyUser

class MyUserAdmin(UserAdmin):
    form = MyUserChangeForm
    add_form = MyUserCreationForm

admin.site.register(MyUser, MyUserAdmin)

When I'm tring to register a new user, the following error is encountered:
Manager isn't available; User has been swapped for 'myuser.MyUser'.
So what can I do to fix it? thanks greatly. I'm using 1.6.1 now.

Change History (2)

comment:1 by charlesw1234@…, 10 years ago

Type: UncategorizedBug

A simplified version of myuser/admin.py has been tried too, same error occuried.

from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from myuser.models import MyUser

# Register your models here.

admin.site.register(MyUser, UserAdmin)

comment:2 by anonymous, 10 years ago

Resolution: fixed
Status: newclosed

Now I know what's happen. django.contrib.auth.models.User can not be used when customized user model is installed.
Any usage of django.contrib.auth.models.User but not django.contrib.auth.get_user_model will cause this error.

For myself, I found django-registration use django.contrib.auth.models.User in forms.py. These forms will raise the error.
After replace User class with the result of get_user_model, all things work fine.

Note: See TracTickets for help on using tickets.
Back to Top