nested relations

3102 ワード

class RetrieveCounselorandAvailableRegionInfoSerializer(serializers.ModelSerializer):
    region_detail = ReadCounselorRegionSerializer(source="counselor_available_region_set", many=True)
    class Meta:
        model = Counselor
        fields = ['name', 'address', 'region_detail']


class RetrieveMatchingSerializer(serializers.ModelSerializer):
    counselor = RetrieveCounselorandAvailableRegionInfoSerializer(source='presession.counselor', read_only=True)
    scheduled_datetime = serializers.CharField(source='presession.scheduled_datetime')

 
    class Meta:
        model = Matching
        fields = ['id', 'matching_notice_status', 'counselor', 'scheduled_datetime' ]
  • に必要なフィールドのみを定義する新しいシーケンス化器.
  • sourceオプションで関連領域を指定します.