I have four models where I need to serialize three into one for my API. I can get the individual serializers to work as expected, but when combining them into one, I do not get the result I was expecting to see.
models.py
class President(models.Model):
name = models.CharField('Name', max_length=50,)
staff = models.ManyToManyField('Member',)
def __str__(self):
return self.name
"""
user creates a new member in this model below
if a new member is an employee, the object is copied into the Employee model
and the user chooses the manager of the employee in the manager field
if a new member is a manager, the object is copied into the Manager model
"""
class Member(models.Model):
president = models.ForeignKey(
President, on_delete=models.CASCADE, related_name='members',
)
manager = models.ForeignKey(
'Manager', on_delete=models.CASCADE, related_name='manager',
)
name = models.CharField('Name', max_length=50,)
email = models.EmailField('Email Address',)
title = models.CharField('Title', max_length=50,)
staff_type = (
('Manager', 'Manager'),
('Employee', 'Employee'),
)
def __str__(self):
return self.name
class Employee(models.Model):
president = models.ForeignKey(
President, on_delete=models.CASCADE, related_name='employeePresident'
)
manager = models.ForeignKey(
'Manager', on_delete=models.CASCADE, related_name='employeeManager'
)
name = models.CharField('Name', max_length=50,)
email = models.EmailField('Email Address',)
title = models.CharField('Title', max_length=50,)
def __str__(self):
return self.name
class Manager(models.Model):
president = models.ForeignKey(
President, on_delete=models.CASCADE, related_name='managerPresident'
)
name = models.CharField('Name', max_length=50,)
department = models.CharField('Department', max_length=50,)
def __str__(self):
return self.name
serializers.py
class PresidentSerializer(serializers.ModelSerializer):
class Meta:
model = President
fields = '__all__'
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
fields = '__all__'
class ManagerSerializer(serializers.ModelSerializer):
members = EmployeeSerializer(many=True,)
class Meta:
model = Manager
fields = ('president', 'name', 'department', 'members')
Up to this point all of these serializers work as expected. The ManagerSerializer exposes the manager's name and the employees underneath them in a tree view like I want.
But, an employee does not necessarily have to be under a manager, for instance they could report directly to the President object, like a Presidents assistant.
How would I combine these three serializers into one where my API would be as follows:
{
"name": "Presidents Name",
"staff": [
{
"name": "Employee No Manager",
"title": "Presidents Asst"
},
{
"name": "John Doe",
"title": "Finance Manager",
"employees": [
{
"name": "Mary Simpson",
"title": "Finance Asst"
}
]
}
}
Not sure if something needs to change in my model design where the President model can take in both Employee and Manager models. Any help would be greatly appreciated.