1π
β
To get a read-only representation of the employee, you can add a SerializerMethodField
. This will leave your employee
field intact for POST
and PUT
requests and add a serialized representation at employee_data
.
class EmployeeSerializer(serializers.ModelSerializer):
class Meta:
model = Employee
class ShiftSerializer(serializers.ModelSerializer):
confirmed_location = LocationSerializer(required=False)
# this will look for a method named get_employee_data
employee_data = serializers.SerializerMethodField()
class Meta:
model = Shift
fields = (
'confirmed_location',
'date_requested',
'shift_requested',
'employee',
'employee_data',
)
def get_employee_data(self, obj):
""" Returns serialized representation of an employee.
"""
return EmployeeSerializer(obj.employee).data
π€sthzg
Source:stackexchange.com