Home > Software design >  Serializer extra field from a ManyToMany Field
Serializer extra field from a ManyToMany Field

Time:10-12

I have these serialiazers

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

and the current representation is like this:

   {
        "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"
            }
        ]
    }

I'd like to keep the department field as IDs and have an extra field for departmentName and have a representation like this:

   {
        "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"
            }
        ]
    }

This is because if I have the 1st representation I have problems in managing the CRUD because department are not IDs.

The problem is because department is a ManyToMany relation, for example if it was just a ForeignKey I could have done like this and it worked.

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

    class Meta:
        model = Doctor
        fields = '__all__'

so, is there a way to achieve this with a ManyToManyField? Or should I use one serializer for visualization and one for add/edit?

CodePudding user response:

You can set a department with a PrimaryKeyRelatedField [drf-doc], and work with the DepartmentSerializer for the departmentName (although it might be better to use plural names):

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

    class Meta:
        model = Doctor
        fields = ('id', 'first_name', 'last_name', 'slug', 'email', 'phone', 'title', 'description', 'image', 'department', 'departmentName')
  • Related