[Answer]-Serializer needs to include more info in a GET while accepting primitives in a POST

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

Leave a comment