diff --git a/employee/forms.py b/employee/forms.py index 2bbff1c87..ee4700b73 100644 --- a/employee/forms.py +++ b/employee/forms.py @@ -194,19 +194,28 @@ class EmployeeForm(ModelForm): def clean(self): super().clean() email = self.cleaned_data["email"] - all_employees = Employee.objects.entire() - exit_employee = all_employees.filter(email=email).first() + query = Employee.objects.entire().filter(email=email) + if self.instance and self.instance.id: + query = query.exclude(id=self.instance.id) + + existing_employee = query.first() + + if existing_employee: + company_id = None + if ( + hasattr(existing_employee, "employee_work_info") + and existing_employee.employee_work_info + ): + company_id = existing_employee.employee_work_info.company_id - if exit_employee: - company_id = getattr( - getattr(exit_employee, "employee_work_info", None), "company_id", None - ) if company_id: error_message = _( - "Employee with this Email already exists in company {}" - ).format(company_id) + "An Employee with this Email already exists in company {}".format( + company_id + ) + ) else: - error_message = _(f"Employee with this Email already exists") + error_message = _("An Employee with this Email already exists") raise forms.ValidationError({"email": error_message})