From e0cf956fe2b63fff19710bc0c12a47e795598b2d Mon Sep 17 00:00:00 2001 From: Arnav Choudhury Date: Fri, 29 Jan 2021 17:55:10 +0530 Subject: [PATCH] Refactored users.forms to make the code more readeable --- .../users/forms.py | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py index 7d3a296b..80cd97ae 100644 --- a/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py +++ b/{{cookiecutter.project_slug}}/{{cookiecutter.project_slug}}/users/forms.py @@ -1,6 +1,5 @@ from django.contrib.auth import forms as admin_forms from django.contrib.auth import get_user_model -from django.core.exceptions import ValidationError from django.utils.translation import gettext_lazy as _ User = get_user_model() @@ -12,20 +11,9 @@ class UserChangeForm(admin_forms.UserChangeForm): class UserCreationForm(admin_forms.UserCreationForm): - - error_message = admin_forms.UserCreationForm.error_messages.update( - {"duplicate_username": _("This username has already been taken.")} - ) - class Meta(admin_forms.UserCreationForm.Meta): model = User - def clean_username(self): - username = self.cleaned_data["username"] - - try: - User.objects.get(username=username) - except User.DoesNotExist: - return username - - raise ValidationError(self.error_messages["duplicate_username"]) + error_messages = { + "username": {"unique": _("This username has already been taken.")} + }