I am trying to sign up new users and add them to an existing group but I am facing this error.

AttributeError: Manager isn't available; 'auth.User' has been swapped for 'main.CustomUser'

first, I thought this error was caused by adding a user to a default group but it seems like from the moment I changed my user model by adding a point field and made migrations the signup is not working as expected

this is my models.py file

class CustomUser(AbstractUser):
    point = models.IntegerField(default=0)

class App(models.Model):
    appname = models.CharField(max_length=255)
    link = models.CharField(max_length=100)
    category= models.CharField(max_length=100)
    subcategory = models.CharField(max_length=100)
    points = models.IntegerField()
    created_at = models.DateTimeField(auto_now_add=True, null=True)
    appicon = models.ImageField(upload_to='icon/')

    def __str__(self):
        return self.appname

this is my signup view

def sign_up(request):
    if request.method == "POST":
        form = RegisterForm(request.POST)
        if form.is_valid():
            user = form.save()
            user_group, created = Group.objects.get_or_create(name='user')
            user.groups.add(user_group)
            login(request, user)
            return redirect('/app')
    else:
        form = RegisterForm()
    return render(request,'registration/signup.html', {'form':form})

i have also added this to my settings.py file AUTH_USER_MODEL = 'main.CustomUser

this is my full traceback

Internal Server Error: /app/signup/
Traceback (most recent call last):
  File "D:\01 tasks\pro 2 and try outs\django main\venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "D:\01 tasks\pro 2 and try outs\django main\venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)    
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^    
  File "D:\01 tasks\pro 2 and try outs\django main\main\views.py", line 87, in 
sign_up
    if form.is_valid():
       ^^^^^^^^^^^^^^^
  File "D:\01 tasks\pro 2 and try outs\django main\venv\Lib\site-packages\django\forms\forms.py", line 197, in is_valid
    return self.is_bound and not self.errors
                                 ^^^^^^^^^^^
  File "D:\01 tasks\pro 2 and try outs\django main\venv\Lib\site-packages\django\forms\forms.py", line 192, in errors
    self.full_clean()
  File "D:\01 tasks\pro 2 and try outs\django main\venv\Lib\site-packages\django\forms\forms.py", line 327, in full_clean
    self._clean_fields()
  File "D:\01 tasks\pro 2 and try outs\django main\venv\Lib\site-packages\django\forms\forms.py", line 342, in _clean_fields
    value = getattr(self, "clean_%s" % name)()
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\01 tasks\pro 2 and try outs\django main\venv\Lib\site-packages\django\contrib\auth\forms.py", line 163, in clean_username
    and self._meta.model.objects.filter(username__iexact=username).exists()    
        ^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\01 tasks\pro 2 and try outs\django main\venv\Lib\site-packages\django\db\models\manager.py", line 196, in __get__
    raise AttributeError(
AttributeError: Manager isn't available; 'auth.User' has been swapped for 'main.CustomUser'
1

There are 1 best solutions below

0
On

this is how I solved this error.

was facing this error when I was using the default user model for my register forms instead of the new custom user model. All I had to do was import the new custom user model from my models.py use it in my register forms and remove the default user import.

both snippets from forms.py file

# from django.contrib.auth.models import User
from .models import App, UploadedImage, CustomUser
from django.contrib.auth.forms import UserCreationForm
class RegisterForm(UserCreationForm):
    email = forms.EmailField(required=True)

    class Meta:
        model = CustomUser 
        #model = User this is the line that caused the error
        fields = ['username', 'email', 'password1', 'password2']

this is in the settings.py file

AUTH_USER_MODEL = 'main.CustomUser'

note you should remove the old User model and change it to your new custom user model wherever you are using it, from your forms.py, models.py, views.py files