[Answered ]-How to serialize the many-to-many fields in DRF?

1👍

The name of the relation in reverse is dataset_info_set by default, since you don’t specify a related_name=… [Django-doc], this is thus the name of the relation:

class literature_dataset_serializer(serializers.ModelSerializer):
    dataset_info_set = dataset_info_serializer(many=True)

    class Meta:
        model = literature_info
        fields = (
            'pmid',
            'dataset_info_set',
        )

Note: Models in Django are written in PascalCase, not snake_case,
so you might want to rename the model from literature_info to LiteratureInfo.

Leave a comment