lucyd
lucyd

Reputation: 1

How to use a single model 'employee' for storing employees and managers in django?

I am creating a ERP type system, where I have a django model(table) called 'employee'.

I want every worker of a company to be an 'employee'. (ex. coder, boss, janitor, line manager etc. will all be 'employee')

I also want some employees to have one or more line manager/s. And a line manager can have multiple employee under him/her.

So the question is how to make line manager a part of 'employee' table, and have a field which describes 'employees' under line manager? So that I can display what line manager/s an employee has. Also display what line manager has which employees under him?

Here's the models file:

class Team(models.Model):
    name = models.CharField(max_length=64)
    t_id = models.CharField(max_length=64)
    def __str__(self):
        return self.name


class Employee(models.Model):
    
    
    first_name=models.CharField(max_length=64)
    
    last_name=models.CharField(max_length=64)
    
    e_id=models.CharField(max_length=64)

    current_role=models.CharField(max_length=64) # "coder"
    
    past_roles=models.CharField(max_length=512) # "coder, developer, janator" (seperator: ,)

    teams = models.ManyToManyField(Team, related_name="employees", blank=True)

    joining_date = models.DateTimeField(null=True,blank=True)

    desk_location=models.CharField(max_length=64)

    #How do I go about this?
    line_manager = ???


    def __str__(self):
        return self.first_name

I am new to django, so please forgive me if I am missing something very simple. Also if there is a more elegant solution which requires me to add additional tables, I dont mind.

Thanks

Upvotes: 0

Views: 1460

Answers (1)

georgekrax
georgekrax

Reputation: 1189

As I understand, you want to have some kind of relationship around those two models. You want an employee to have one or multiple line managers, as well as a manager to have one or multiple employees. I would recommend you to search and find tutorials on YouTube about the Many-to-many Relationships and the field that Django provides for this type of relationship models.ManyToManyField().

Here is the link for Django documentation on Many-to-many relationships between models: https://docs.djangoproject.com/en/3.0/topics/db/examples/many_to_many/.

However, to implement such kind of functionality you will need to have another model for LineManagers themselves.

I hope that helps. Good luck with your project!

Upvotes: 1

Related Questions