api changes.

This commit is contained in:
jonaswinkler 2020-12-03 19:56:52 +01:00
parent 62cc4a7a54
commit 68c233005e
2 changed files with 22 additions and 18 deletions

View File

@ -79,11 +79,15 @@ class DocumentTypeField(serializers.PrimaryKeyRelatedField):
class DocumentSerializer(serializers.ModelSerializer): class DocumentSerializer(serializers.ModelSerializer):
correspondent_id = CorrespondentField( correspondent = CorrespondentField(allow_null=True)
allow_null=True, source='correspondent') tags = TagsField(many=True)
tags_id = TagsField(many=True, source='tags') document_type = DocumentTypeField(allow_null=True)
document_type_id = DocumentTypeField(
allow_null=True, source='document_type') 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: class Meta:
model = Document model = Document
@ -91,13 +95,13 @@ class DocumentSerializer(serializers.ModelSerializer):
fields = ( fields = (
"id", "id",
"correspondent", "correspondent",
"correspondent_id", "correspondent_object",
"document_type", "document_type",
"document_type_id", "document_type_object",
"title", "title",
"content", "content",
"tags", "tags",
"tags_id", "tags_objects",
"created", "created",
"modified", "modified",
"added", "added",

View File

@ -41,20 +41,20 @@ class TestDocumentApi(DirectoriesMixin, APITestCase):
returned_doc = response.data['results'][0] returned_doc = response.data['results'][0]
self.assertEqual(returned_doc['id'], doc.id) self.assertEqual(returned_doc['id'], doc.id)
self.assertEqual(returned_doc['title'], doc.title) self.assertEqual(returned_doc['title'], doc.title)
self.assertEqual(returned_doc['correspondent']['name'], c.name) self.assertEqual(returned_doc['correspondent_object']['name'], c.name)
self.assertEqual(returned_doc['document_type']['name'], dt.name) self.assertEqual(returned_doc['document_type_object']['name'], dt.name)
self.assertEqual(returned_doc['correspondent']['id'], c.id) self.assertEqual(returned_doc['correspondent_object']['id'], c.id)
self.assertEqual(returned_doc['document_type']['id'], dt.id) self.assertEqual(returned_doc['document_type_object']['id'], dt.id)
self.assertEqual(returned_doc['correspondent']['id'], returned_doc['correspondent_id']) self.assertEqual(returned_doc['correspondent_object']['id'], returned_doc['correspondent'])
self.assertEqual(returned_doc['document_type']['id'], returned_doc['document_type_id']) self.assertEqual(returned_doc['document_type_object']['id'], returned_doc['document_type'])
self.assertEqual(len(returned_doc['tags']), 1) self.assertEqual(len(returned_doc['tags']), 1)
self.assertEqual(returned_doc['tags'][0]['name'], tag.name) self.assertEqual(returned_doc['tags_objects'][0]['name'], tag.name)
self.assertEqual(returned_doc['tags'][0]['id'], tag.id) self.assertEqual(returned_doc['tags_objects'][0]['id'], tag.id)
self.assertListEqual(returned_doc['tags_id'], [tag.id]) self.assertListEqual(returned_doc['tags'], [tag.id])
c2 = Correspondent.objects.create(name="c2") c2 = Correspondent.objects.create(name="c2")
returned_doc['correspondent_id'] = c2.pk returned_doc['correspondent'] = c2.pk
returned_doc['title'] = "the new title" returned_doc['title'] = "the new title"
response = self.client.put('/api/documents/{}/'.format(doc.pk), returned_doc, format='json') response = self.client.put('/api/documents/{}/'.format(doc.pk), returned_doc, format='json')