def test_DocumentLinkSerializer(self): doc_link = iati_factory.DocumentLinkFactory.create() intermediate = iati_factory.DocumentLinkCategoryFactory.build( document_link=doc_link) serializer = serializers.DocumentLinkSerializer(doc_link) assert serializer.data['url'] == doc_link.url,\ """ 'document_link.url' should be serialized to a field called 'url' """ assert 'format' and 'category' and 'title' in serializer.data,\ """ a serialized document_link should also contain the fields 'format' 'category' and 'title' """ assert type(serializer.fields['format']) is serializers.CodelistSerializer,\ """ the field 'format' should be a CodelistSerializer """ assert type(serializer.fields['categories'].child) is serializers.\ DocumentCategorySerializer,\ """ the field 'categories' should be a DocumentCategorySerializer """ assert type(serializer.fields['title'].child) is serializers.\ NarrativeContainerSerializer,\ """
class ResultSerializer(serializers.ResultSerializer): document_links = serializers.DocumentLinkSerializer( many=True, read_only=True, source='documentlink_set', fields=[ 'url', 'format', 'categories', 'languages', 'title', 'document_date', 'description' ]) sectors = ActivitySectorSerializer( many=True, source='activity.activitysector_set', read_only=True, required=False, ) recipient_countries = RecipientCountrySerializer( many=True, source='activity.activityrecipientcountry_set', read_only=True, required=False, ) recipient_regions = ActivityRecipientRegionSerializer( many=True, source='activity.activityrecipientregion_set', read_only=True, required=False, ) class Meta: model = Result fields = ('title', 'description', 'indicator', 'type', 'aggregation_status', 'document_links', 'iati_identifier', 'sectors', 'recipient_countries', 'recipient_regions', 'iati_identifier')