Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

i have problem with my code when i want signup error appear Manager isn't available; 'auth.User' has been swapped for 'members.CustomUser' , i try solotion of other questions same like Manager isn't available; 'auth.User' has been swapped for 'members.CustomUser' but all of them asking to replace User = User = get_user_model() but i am not use any User in my code or i dont know where i used that.im new in django , python , js and etc so if my question is silly forgivme .

for more information :1) i used Django Signup Tutorial for create signup method . first that was working well but after i expand my homework project i get error .2)in others app ("products" and "search") no where im not import User and even i dont use CustomUser too bez not need to work with User in this apps.just memebers work with User and CustomUser.

model.py :

from django.contrib.auth.models import AbstractUser



class CustomUser(AbstractUser):

    def __str__(self):
        return self.email

    class Meta:
        verbose_name = "member"
        verbose_name_plural = "members"

setting.py:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'card.apps.CardConfig',
    'members.apps.MembersConfig',
    'search.apps.SearchConfig',
    'products.apps.ProductsConfig',
    'rest_framework',
]

AUTH_USER_MODEL = 'members.CustomUser'

admin.py:

from django.contrib import admin
from django.contrib.auth import get_user_model
from django.contrib.auth.admin import UserAdmin

from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser



class CustomUserAdmin(UserAdmin):
    add_form = CustomUserCreationForm
    form = CustomUserChangeForm
    model = CustomUser
    list_display = ['email', 'username']


admin.site.register(CustomUser, CustomUserAdmin)

form.py:

# users/forms.py
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser


class CustomUserCreationForm(UserCreationForm):
    class Meta(UserCreationForm):
        model = CustomUser
        fields = ('username', 'email')


class CustomUserChangeForm(UserChangeForm):
    class Meta:
        model = CustomUser
        fields = ('username', 'email')

error:

27/Feb/2019 12:36:01] "GET /signup/ HTTP/1.1" 200 5293
Internal Server Error: /signup/
Traceback (most recent call last):
  File "C:\shopping\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\shopping\venv\lib\site-packages\django\core\handlers\base.py", line 126, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\shopping\venv\lib\site-packages\django\core\handlers\base.py", line 124, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\shopping\venv\lib\site-packages\django\views\generic\base.py", line 68, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\shopping\venv\lib\site-packages\django\views\generic\base.py", line 88, in dispatch
    return handler(request, *args, **kwargs)
  File "C:\shopping\venv\lib\site-packages\django\views\generic\edit.py", line 172, in post
    return super().post(request, *args, **kwargs)
  File "C:\shopping\venv\lib\site-packages\django\views\generic\edit.py", line 141, in post
    if form.is_valid():
  File "C:\shopping\venv\lib\site-packages\django\forms\forms.py", line 185, in is_valid
    return self.is_bound and not self.errors
  File "C:\shopping\venv\lib\site-packages\django\forms\forms.py", line 180, in errors
    self.full_clean()
  File "C:\shopping\venv\lib\site-packages\django\forms\forms.py", line 383, in full_clean
    self._post_clean()
  File "C:\shopping\venv\lib\site-packages\django\contrib\auth\forms.py", line 107, in _post_clean
    super()._post_clean()
  File "C:\shopping\venv\lib\site-packages\django\forms\models.py", line 403, in _post_clean
    self.instance.full_clean(exclude=exclude, validate_unique=False)
  File "C:\shopping\venv\lib\site-packages\django\db\models\base.py", line 1137, in full_clean
    self.clean()
  File "C:\shopping\venv\lib\site-packages\django\contrib\auth\models.py", line 338, in clean
    self.email = self.__class__.objects.normalize_email(self.email)
  File "C:\shopping\venv\lib\site-packages\django\db\models\manager.py", line 188, in __get__
    cls._meta.swapped,
AttributeError: Manager isn't available; 'auth.User' has been swapped for 'members.CustomUser'
[27/Feb/2019 12:36:04] "POST /signup/ HTTP/1.1" 500 113770
like image 897
mehdi Avatar asked Feb 27 '19 08:02

mehdi


2 Answers

Modify views.py

from django.contrib.auth.forms import UserCreationForm
from django.urls import reverse_lazy
from django.views import generic


class SignUp(generic.CreateView):
    form_class = UserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'signup.html'

to

from .forms import CustomUserCreationForm
from django.urls import reverse_lazy
from django.views import generic


class SignUp(generic.CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy('login')
    template_name = 'signup.html'
like image 182
Rexcirus Avatar answered Nov 12 '22 07:11

Rexcirus


In your forms.py make changes as:

from django.contrib.auth import get_user_model

class CustomUserChangeForm(UserChangeForm):
    class Meta:
        model = get_user_model()
        fields = ('username', 'email')

class CustomUserCreationForm(UserCreationForm):
    class Meta:
        model = get_user_model()
        fields = ('username', 'email')

Also add this script in your members/init.py file:

    default_app_config = 'members.apps.MembersConfig'
like image 4
Sanip Avatar answered Nov 12 '22 08:11

Sanip