我有四个模型,需要为我的API序列化三个模型。我可以使各个序列化器按预期工作,但是将它们组合为一个时,却没有得到我期望看到的结果。
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')
到目前为止,所有这些串行器均按预期工作。 ManagerSerializer在树状视图中显示经理的姓名和他们下面的员工。
但是,员工不必一定要在经理的领导下,例如,他们可以像总裁助理一样直接向总裁对象报告。
我如何将这三个串行器组合成一个我的API如下:
{
"name": "Presidents Name",
"staff": [
{
"name": "Employee No Manager",
"title": "Presidents Asst"
},
{
"name": "John Doe",
"title": "Finance Manager",
"employees": [
{
"name": "Mary Simpson",
"title": "Finance Asst"
}
]
}
}
不确定我的模型设计中是否需要更改,总统模型可以同时适用于员工模型和经理模型。任何帮助将不胜感激。
答案 0 :(得分:0)
我会将您的数据库结构简化为每位员工仅1个类(以及2个用于数据库规范化的额外类):
class JobTitle(models.Model):
title = models.CharField('Title', max_length=50)
def __str__(self):
return self.title
class Department(models.Model):
name = models.CharField('Department', max_length=50)
def __str__(self):
return self.name
class Employee(models.Model):
name = models.CharField('Name', max_length=50)
boss = models.ForeignKey("Employee", on_delete=models.CASCADE, related_name="staff", blank=True, null=True)
department = models.ForeignKey("Department", on_delete=models.CASCADE)
email = models.EmailField('Email Address')
title = models.ForeignKey("JobTitle", on_delete=models.CASCADE)
@property
def is_president(self):
return self.boss is None
@property
def is_manager(self):
return len(self.staff) > 0
def __str__(self):
return self.name
然后,您可以使用this answer中的递归序列化器:
class RecursiveField(serializers.Serializer):
def to_representation(self, value):
serializer = self.parent.parent.__class__(value, context=self.context)
return serializer.data
class EmployeeSerializer(serializers.ModelSerializer):
staff = RecursiveField(many=True)
class Meta:
model = Employee
fields = '__all__'