Hi,
how can I display only the UniqueConstraint error next to the field? Do I have to override the save method?
In my manage.py, I created a Model with a UniqueConstraint
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(
_("email address"),
unique=True,
# Previos normal errormesage displayed along side the input
# error_messages={
# "unique": _("A user with this email address already exists."),
# },
)
# ...
class Meta:
constraints = [
UniqueConstraint(
Lower("email"),
name="user_email_ci_uniqueness",
),
]
using this template:
signupForm.html:
<form id="form" method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form }}
<input type="submit" value="Sign up">
</form>
addtional infos (urls.py, views.py, forms.py):
urls.py:
urlpatterns = [
path("signup/",
views.SignupView.as_view(),
name="sign_up"),
]
views.py:
class SignupView(CreateView):
form_class = SignupForm
model = User
template_name = "users/signupForm.html"
success_url = "/"
forms.py:
class SignupForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = User
fields = ("email",)
I’m using Django 4.2 and I couldn’t find anything on this.
Thank you