From 227a48b285947c93ce1ba6f634dce599af02393a Mon Sep 17 00:00:00 2001 From: Faheed Date: Sun, 30 Nov 2025 12:49:48 +0300 Subject: [PATCH] agency unique email contraint fail solved --- recruitment/forms.py | 28 ++++++++++++++++------------ recruitment/views.py | 1 + 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/recruitment/forms.py b/recruitment/forms.py index 75561f4..f2db1ce 100644 --- a/recruitment/forms.py +++ b/recruitment/forms.py @@ -27,7 +27,8 @@ from .models import ( Participants, Message, Person, - Document + Document, + CustomUser ) # from django_summernote.widgets import SummernoteWidget @@ -1028,17 +1029,20 @@ class HiringAgencyForm(forms.ModelForm): raise ValidationError("Please enter a valid email address.") # Check uniqueness (optional - remove if multiple agencies can have same email) - instance = self.instance - if not instance.pk: # Creating new instance - if HiringAgency.objects.filter(email=email).exists(): - raise ValidationError("An agency with this email already exists.") - else: # Editing existing instance - if ( - HiringAgency.objects.filter(email=email) - .exclude(pk=instance.pk) - .exists() - ): - raise ValidationError("An agency with this email already exists.") + # instance = self.instance + email = email.lower().strip() + if CustomUser.objects.filter(email=email).exists(): + raise ValidationError("This email is already associated with a user account.") + # if not instance.pk: # Creating new instance + # if HiringAgency.objects.filter(email=email).exists(): + # raise ValidationError("An agency with this email already exists.") + # else: # Editing existing instance + # if ( + # HiringAgency.objects.filter(email=email) + # .exclude(pk=instance.pk) + # .exists() + # ): + # raise ValidationError("An agency with this email already exists.") return email.lower().strip() if email else email def clean_phone(self): diff --git a/recruitment/views.py b/recruitment/views.py index f366432..8241d1c 100644 --- a/recruitment/views.py +++ b/recruitment/views.py @@ -3313,6 +3313,7 @@ def agency_create(request): if request.method == "POST": form = HiringAgencyForm(request.POST) if form.is_valid(): + agency = form.save() messages.success(request, f'Agency "{agency.name}" created successfully!') return redirect("agency_detail", slug=agency.slug)