From 68c233005ef5654620a0ab5f2a014cb7b935d84e Mon Sep 17 00:00:00 2001 From: jonaswinkler Date: Thu, 3 Dec 2020 19:56:52 +0100 Subject: [PATCH] api changes. --- src/documents/serialisers.py | 20 ++++++++++++-------- src/documents/tests/test_api.py | 20 ++++++++++---------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/documents/serialisers.py b/src/documents/serialisers.py index 14102df5c..e6f278f93 100644 --- a/src/documents/serialisers.py +++ b/src/documents/serialisers.py @@ -79,11 +79,15 @@ class DocumentTypeField(serializers.PrimaryKeyRelatedField): class DocumentSerializer(serializers.ModelSerializer): - correspondent_id = CorrespondentField( - allow_null=True, source='correspondent') - tags_id = TagsField(many=True, source='tags') - document_type_id = DocumentTypeField( - allow_null=True, source='document_type') + correspondent = CorrespondentField(allow_null=True) + tags = TagsField(many=True) + document_type = DocumentTypeField(allow_null=True) + + correspondent_object = TagSerializer( + read_only=True, source="correspondent") + document_type_object = TagSerializer( + read_only=True, source="document_type") + tags_objects = TagSerializer(many=True, read_only=True, source="tags") class Meta: model = Document @@ -91,13 +95,13 @@ class DocumentSerializer(serializers.ModelSerializer): fields = ( "id", "correspondent", - "correspondent_id", + "correspondent_object", "document_type", - "document_type_id", + "document_type_object", "title", "content", "tags", - "tags_id", + "tags_objects", "created", "modified", "added", diff --git a/src/documents/tests/test_api.py b/src/documents/tests/test_api.py index e2e1b254e..adfce313f 100644 --- a/src/documents/tests/test_api.py +++ b/src/documents/tests/test_api.py @@ -41,20 +41,20 @@ class TestDocumentApi(DirectoriesMixin, APITestCase): returned_doc = response.data['results'][0] self.assertEqual(returned_doc['id'], doc.id) self.assertEqual(returned_doc['title'], doc.title) - self.assertEqual(returned_doc['correspondent']['name'], c.name) - self.assertEqual(returned_doc['document_type']['name'], dt.name) - self.assertEqual(returned_doc['correspondent']['id'], c.id) - self.assertEqual(returned_doc['document_type']['id'], dt.id) - self.assertEqual(returned_doc['correspondent']['id'], returned_doc['correspondent_id']) - self.assertEqual(returned_doc['document_type']['id'], returned_doc['document_type_id']) + self.assertEqual(returned_doc['correspondent_object']['name'], c.name) + self.assertEqual(returned_doc['document_type_object']['name'], dt.name) + self.assertEqual(returned_doc['correspondent_object']['id'], c.id) + self.assertEqual(returned_doc['document_type_object']['id'], dt.id) + self.assertEqual(returned_doc['correspondent_object']['id'], returned_doc['correspondent']) + self.assertEqual(returned_doc['document_type_object']['id'], returned_doc['document_type']) self.assertEqual(len(returned_doc['tags']), 1) - self.assertEqual(returned_doc['tags'][0]['name'], tag.name) - self.assertEqual(returned_doc['tags'][0]['id'], tag.id) - self.assertListEqual(returned_doc['tags_id'], [tag.id]) + self.assertEqual(returned_doc['tags_objects'][0]['name'], tag.name) + self.assertEqual(returned_doc['tags_objects'][0]['id'], tag.id) + self.assertListEqual(returned_doc['tags'], [tag.id]) c2 = Correspondent.objects.create(name="c2") - returned_doc['correspondent_id'] = c2.pk + returned_doc['correspondent'] = c2.pk returned_doc['title'] = "the new title" response = self.client.put('/api/documents/{}/'.format(doc.pk), returned_doc, format='json')