Manager isn't available; User has been swapped for 'pet.Person'

前端 未结 4 650
误落风尘
误落风尘 2020-11-28 05:55

I\'m been using the default user model in django for quite a abit and I realize , if I need to further enhance it , I would have to create my own custom User Model in django

相关标签:
4条回答
  • 2020-11-28 06:07

    For anyone else who might come across this problem, I also solved it by simply doing this on forms.py:

    add this at the top of the forms.py file

    from .models import YourCustomUser
    

    and then add this to your forms.py CustomUser form:

    class SignUpForm(UserCreationForm):
    #profile_year        = blaaa blaa blaaa irrelevant.. You have your own stuff here don't worry about it
    
       # here is the important part.. add a class Meta-
       class Meta:
          model = YourCustomUser #this is the "YourCustomUser" that you imported at the top of the file  
          fields = ('username', 'password1', 'password2', #etc etc, other fields you want displayed on the form)
    

    BIG NOTES, ATTENTION:

    1. This code worked for my case. I have a view for signing users up, I had a problem here and I solved it, I haven't tried it for logging in users.

    2. The include = () part is required, or you can add exclude = (), but you have to have one

    0 讨论(0)
  • 2020-11-28 06:08

    All the solutions provided above did not work in my case. If you using Django version 3.1 there is another solution for you:

    In auth/forms, comment out line 10 and change the model in line 104 & 153 to your defined model.

    0 讨论(0)
  • 2020-11-28 06:09

    The problem is that User refers to django.contrib.auth.models.User and now you have got a Custom User pet.Person assuming you have in the settings.py

    AUTH_USER_MODEL = "pet.Person"
    

    you have to define User with the Custom User model and you can do this with get_user_model at the top of the file where you use User

    from django.contrib.auth import get_user_model
    User = get_user_model()
    

    now you will be able to use Custom User model and the problem has been fixed.

    0 讨论(0)
  • 2020-11-28 06:18

    Important caveat to update the above solutions... If you're facing this kind of problem, you've probably tried various solutions around the web telling you to add AUTH_USER_MODEL = users.CustomUser to settings.py and then to add the following code to views.py forms.py and any other file that calls User:

    from django.contrib.auth import get_user_model
    User = get_user_model()
    

    And then you scratch your head when you get the error:

    Manager isn't available; 'auth.User' has been swapped for 'users.User'
    

    Anytime your code references User such as:

    User.objects.get()
    

    Cause you know you already put objects = UserManager() in your custom user class (UserManager being the name of your custom manager that extends BaseUserManager).

    Well as it turns out doing:

    User = get_user_model() # somewhere at the top of your .py file
    # followed by
    User.objects.get() # in a function/method of that same file
    

    Is NOT equivalent to:

    get_user_model().objects.get() # without the need for User = get_user_model() anywhere
    

    Perhaps not intuitive, but it turns out that that in python, executing User = get_user_model() once at the time of import does not then result in User being defined across subsequent calls (i.e. it does not turn User into a "constant" of sorts which you might expect if you're coming from a C/C++ background; meaning that the execution of User = get_user_model() occurs at the time of imports, but is then de-referenced before subsequent called to class or function/method in that file).

    So to sum up, in all files that reference the User class (e.g. calling functions or variables such as User.objects.get() User.objects.all() User.DoesNotExist etc...):

    # Add the following import line
    from django.contrib.auth import get_user_model
    
    # Replace all references to User with get_user_model() such as...
    user = get_user_model().objects.get(pk=uid)
    # instead of  user = User.objects.get(pk=uid)
    # or
    queryset = get_user_model().objects.all()
    # instead of queryset = User.objects.all()
    # etc...
    

    Hope this helps save others some time...

    0 讨论(0)
提交回复
热议问题