44 lines
1.6 KiB
Python
44 lines
1.6 KiB
Python
|
|
from rest_framework import serializers
|
||
|
|
from .models import Position, Department, Employee
|
||
|
|
|
||
|
|
class PositionSerializer(serializers.ModelSerializer):
|
||
|
|
class Meta:
|
||
|
|
model = Position
|
||
|
|
fields = "__all__"
|
||
|
|
|
||
|
|
|
||
|
|
class DepartmentSerializer(serializers.ModelSerializer):
|
||
|
|
class Meta:
|
||
|
|
model = Department
|
||
|
|
fields = "__all__"
|
||
|
|
|
||
|
|
|
||
|
|
class EmployeeSerializer(serializers.ModelSerializer):
|
||
|
|
first_name = serializers.CharField(source="account.first_name")
|
||
|
|
last_name = serializers.CharField(source="account.last_name")
|
||
|
|
middle_name = serializers.CharField(source="account.middle_name", allow_null=True)
|
||
|
|
phone = serializers.CharField(source="account.phone", allow_null=True)
|
||
|
|
email = serializers.CharField(source="account.email")
|
||
|
|
|
||
|
|
class Meta:
|
||
|
|
model = Employee
|
||
|
|
fields = [
|
||
|
|
"id",
|
||
|
|
"department",
|
||
|
|
"position",
|
||
|
|
"first_name",
|
||
|
|
"last_name",
|
||
|
|
"middle_name",
|
||
|
|
"phone",
|
||
|
|
"email",
|
||
|
|
]
|
||
|
|
|
||
|
|
def update(self, instance, validated_data):
|
||
|
|
account_data = validated_data.pop("account", {})
|
||
|
|
instance.account.first_name = account_data.get("first_name", instance.account.first_name)
|
||
|
|
instance.account.last_name = account_data.get("last_name", instance.account.last_name)
|
||
|
|
instance.account.middle_name = account_data.get("middle_name", instance.account.middle_name)
|
||
|
|
instance.account.phone = account_data.get("phone", instance.account.phone)
|
||
|
|
instance.account.email = account_data.get("email", instance.account.email)
|
||
|
|
instance.account.save()
|
||
|
|
return super().update(instance, validated_data)
|