来自 ManyToMany 字段的序列化程序额外字段

Serializer extra field from a ManyToMany Field

我有这些序列化器

class DepartmentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Department
        fields = ('name', 'slug', 'image', 'description')


class DoctorSerializer(serializers.ModelSerializer):

    class Meta:
        model = Doctor
        fields = '__all__'
        depth = 1

而现在的表示是这样的:

   {
        "id": 1,
        "first_name": "Jhon",
        "last_name": "Doe",
        "slug": "jhon-doe",
        "email": null,
        "phone": null,
        "title": null,
        "description": "",
        "image": "http://localhost:8000/media/studio/default.jpg",
        "department": [
            {
                "id": 1,
                "name": "Acupuncture",
                "slug": "Acupuncture",
                "image": "http://localhost:8000/media/studio/acupuncture-min.jpg",
                "description": "Acupuncture"
            },
            {
                "id": 3,
                "name": "Cardiology",
                "slug": "cardiology",
                "image": "http://localhost:8000/media/studio/default.jpg",
                "description": "Cardiology"
            }
        ]
    }

我想将 department 字段保留为 ID,并为 departmentName 增加一个字段,并采用如下表示形式:

   {
        "id": 1,
        "first_name": "Jhon",
        "last_name": "Doe",
        "slug": "jhon-doe",
        "email": null,
        "phone": null,
        "title": null,
        "description": "",
        "image": "http://localhost:8000/media/studio/default.jpg",
        "department": [
            1,
            3
        ]
        "departmentName": [
            {
                "id": 1,
                "name": "Acupuncture",
                "slug": "Acupuncture",
                "image": "http://localhost:8000/media/studio/acupuncture-min.jpg",
                "description": "Acupuncture"
            },
            {
                "id": 3,
                "name": "Cardiology",
                "slug": "cardiology",
                "image": "http://localhost:8000/media/studio/default.jpg",
                "description": "Cardiology"
            }
        ]
    }

这是因为如果我有第一个表示,我在管理 CRUD 时会遇到问题,因为 department 不是 ID。

问题是因为 department 是多对多关系,例如,如果它只是一个 ForeignKey,我可以这样做并且有效。

class DoctorSerializer(serializers.ModelSerializer):
    departmentName = serializers.CharField(read_only=True, source="department.name")

    class Meta:
        model = Doctor
        fields = '__all__'

所以,有没有办法用 ManyToManyField 来实现这个目标?或者我应该使用一个序列化器进行可视化,另一个用于 add/edit?

您可以将 department 设置为 PrimaryKeyRelatedField [drf-doc],并将 DepartmentSerializer 设置为 departmentName(尽管使用复数名称可能更好):

class DoctorSerializer(serializers.ModelSerializer):
    <strong>department</strong> = serializers.<strong>PrimaryKeyRelatedField(many=True, read_only=True)</strong>
    <strong>departmentName</strong> = DepartmentSerializer(many=True, source='department')

    class Meta:
        model = Doctor
        fields = ('id', 'first_name', 'last_name', 'slug', 'email', 'phone', 'title', 'description', 'image', <strong>'department', 'departmentName'</strong>)