Search code examples
pythondjangodjango-modelsormentity-relationship

Checking condition before inserting data to a table in Django


I have 5 tables User(i.e Employee), Positions and Emp_position(which is a relationship table between the Employee and Positions). I have a department table which contains the list of all departments in my company and a relationship table Works_in which maps User(Employee) to a department. Now in the Works_in table when I am mapping User(Employee) to a Department I want to check whether the data that I am inserting in Works_in(relationship) table. That is mapping an employee to a department already has a manager mapped to it or not.

Example :

Employee

Himanshu
Bassi
BB

Position

Developer
Manager
Tester

Emp_position(relationship table)

Employee    Position
Himanshu    Developer
Bassi       Manager
BB          Manager

Department

Web UI
Data Analysis
Machine Learning

Works_in(relationship table)

Employee    Department
Himanshu    Web UI
Bassi       Web UI
BB          Web UI    # this is wrong, which needs to be checked

Each Employee will be mapped to a Department, But a department should not have multiple manager. Over here since Bassi and BB are manager we don't want them to be mapped to same department. So for that purpose I want to check before the data BB Web UI was being inserted to our Works_in table whether the department that is going to be mapped to a department already has a manager mapped to it. So I want to include a check before inserting data to my Works_in table.

Here is my models.py file.

from django.contrib.auth.models import User

class Position(models.Model):
    position_name = models.CharField(max_length=20, unique=True)

    def __str__(self):
        return self.position_name


class Emp_position(models.Model):
    emp_uname = models.ForeignKey(User, related_name='emp_name', to_field='username', on_delete=models.CASCADE, unique=True)
    position_name = models.ForeignKey(Position, related_name='position', to_field='position_name', on_delete=models.CASCADE)

    def __str__(self):
        return str(self.emp_uname) + " " + str(self.position_name)


class Department(models.Model):
    name = models.CharField(max_length=20, unique=True)

    def __str__(self):
        return self.name

class Works_in(models.Model):
    emp_name = models.ForeignKey(User, related_name='works_emp_name', to_field='username', on_delete=models.CASCADE)
    dept_name = models.ForeignKey(Department, blank=False, null=False, to_field='name',related_name='works_on_dept', on_delete=models.CASCADE)

    def __str__(self):
        return str(self.emp_name) + " " + str(self.dept_name)

Solution

  • After searching and doing some research related to my needs as mentioned in the question. We can override clean method inside the model for this and raise an error inside the clean method in case if the data entered does not satisfy our logic

    def clean(self):
        # statements
        if condition:
             raise ValidationError('Error message')