django Manager isn't available; 'auth.User' has been swapped for 'users.User'
djangoでアカウント作成機能を追加していますが、エラーを解決できません。
カスタムユーザーを使用しています。
ネットの情報を参考にしています。
わかるかたお願いします。
Internal Server Error: /accounts/signup/
Traceback (most recent call last):
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/views/generic/edit.py", line 172, in post
return super().post(request, *args, **kwargs)
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/views/generic/edit.py", line 141, in post
if form.is_valid():
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/forms/forms.py", line 185, in is_valid
return self.is_bound and not self.errors
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/forms/forms.py", line 180, in errors
self.full_clean()
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/forms/forms.py", line 383, in full_clean
self._post_clean()
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/contrib/auth/forms.py", line 107, in _post_clean
super()._post_clean()
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/forms/models.py", line 403, in _post_clean
self.instance.full_clean(exclude=exclude, validate_unique=False)
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/db/models/base.py", line 1188, in full_clean
self.clean()
File "/anaconda3/envs/active/lib/python3.7/site-packages/django/contrib/auth/models.py", line 339, in clean
self.email = self.__class__.objects.normalize_email(self.email)
File "/anaconda3/envs/active/lib/python3.7/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 'users.User'
<form action="" method="POST">
{{ form.non_field_errors }}
{% for field in form %}
<div class="form-group">
<label for="{{ field.id_for_label }}">{{ field.label_tag }}</label>
{{ field }}
{{ field.errors }}
</div>
{% endfor %}
{% csrf_token %}
<button type="submit" class="btn btn-primary btn-lg">送信</button>
</form>
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model
User = get_user_model()
class SignUpForm(UserCreationForm):
"""ユーザー登録用フォーム"""
class Meta:
model = User
fields = ('email',)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in self.fields.values():
field.widget.attrs['class'] = 'form-control'
def clean_email(self):
email = self.cleaned_data['email']
User.objects.filter(email=email, is_active=False).delete()
return email
class SignUp(CreateView):
form_class = UserCreationForm
template_name = "accounts/signup.html"
success_url = reverse_lazy('')
def form_valid(self, form):
user = form.save(commit=False)
user.is_active = False
user.save()
# アクティベーションURLの送付
current_site = get_current_site(self.request)
#domeinの取得
domain = current_site.domain
context = {
'protocol': self.request.scheme,
'domain': domain,
'token': dumps(user.pk),
'user': user,
}
#templateを呼び出す
subject = render_to_string('mail/create/subject.txt', context)
message = render_to_string('mail/create/message.txt', context)
user.email_user(subject, message)
return redirect('accounts:SignUpDone')
class SignUpDone(TemplateView):
"""ユーザー仮登録したよ"""
template_name = 'accounts/signupdone.html'
class SignUpComplete(TemplateView):
"""メール内URLアクセス後のユーザー本登録"""
template_name = 'accounts/signupcomplete.html'
timeout_seconds = getattr(settings, 'ACTIVATION_TIMEOUT_SECONDS', 60*60*24) # デフォルトでは1日以内
def get(self, request, **kwargs):
"""tokenが正しければ本登録."""
token = kwargs.get('token')
try:
user_pk = loads(token, max_age=self.timeout_seconds)
# 期限切れ
except SignatureExpired:
return HttpResponseBadRequest()
# tokenが間違っている
except BadSignature:
return HttpResponseBadRequest()
# tokenは問題なし
else:
try:
user = User.objects.get(pk=user_pk)
except User.DoesNotExist:
return HttpResponseBadRequest()
else:
if not user.is_active:
# 問題なければ本登録とする
user.is_active = True
user.save()
return super().get(request, **kwargs)
return HttpResponseBadRequest()