From 7c4de2d8a1983c058bc6b8fc184da73652c9d40e Mon Sep 17 00:00:00 2001 From: Jamie Bliss Date: Fri, 30 Jun 2017 11:09:22 -0400 Subject: [PATCH 1/3] Add example-specific requirements --- example/requirements.txt | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 example/requirements.txt diff --git a/example/requirements.txt b/example/requirements.txt new file mode 100644 index 00000000..a9660ae7 --- /dev/null +++ b/example/requirements.txt @@ -0,0 +1,2 @@ +# Requirements specifically for the example app +packaging From d61c7656ca19fe720c9383f5e7c33bf7bbbc8511 Mon Sep 17 00:00:00 2001 From: Jamie Bliss Date: Fri, 30 Jun 2017 11:10:06 -0400 Subject: [PATCH 2/3] Document installing additional requirements --- docs/getting-started.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/docs/getting-started.md b/docs/getting-started.md index 3dc4ef6e..336f4486 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -69,7 +69,9 @@ From Source ## Running the example app git clone https://github.com/django-json-api/django-rest-framework-json-api.git - cd django-rest-framework-json-api && pip install -e . + cd django-rest-framework-json-api + pip install -e . + pip install -r example/requirements.txt django-admin.py runserver Browse to http://localhost:8000 From 95bfebede42904c26c0fe0a39bbb5b0c50c7df15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20S?= Date: Mon, 3 Jul 2017 22:11:47 +0200 Subject: [PATCH 3/3] Fix polymorphic serializers without any model instances as data --- .../tests/integration/test_polymorphism.py | 24 +++++++++++++++++++ rest_framework_json_api/serializers.py | 2 +- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/example/tests/integration/test_polymorphism.py b/example/tests/integration/test_polymorphism.py index e5e80290..2aa2091b 100644 --- a/example/tests/integration/test_polymorphism.py +++ b/example/tests/integration/test_polymorphism.py @@ -78,6 +78,30 @@ def test_polymorphism_on_polymorphic_model_list_post(client): assert content['data']['attributes']['artist'] == test_artist +def test_polymorphic_model_without_any_instance(client): + expected = { + "links": { + "first": "http://testserver/projects?page=1", + "last": "http://testserver/projects?page=1", + "next": None, + "prev": None + }, + "data": [], + "meta": { + "pagination": { + "page": 1, + "pages": 1, + "count": 0 + } + } + } + + response = client.get(reverse('project-list')) + assert response.status_code == 200 + content = load_json(response.content) + assert expected == content + + def test_invalid_type_on_polymorphic_model(client): test_topic = 'New test topic {}'.format(random.randint(0, 999999)) test_artist = 'test-{}'.format(random.randint(0, 999999)) diff --git a/rest_framework_json_api/serializers.py b/rest_framework_json_api/serializers.py index 66d6add7..e4a29e1f 100644 --- a/rest_framework_json_api/serializers.py +++ b/rest_framework_json_api/serializers.py @@ -220,7 +220,7 @@ def get_fields(self): """ Return an exhaustive list of the polymorphic serializer fields. """ - if self.instance is not None: + if self.instance not in (None, []): if not isinstance(self.instance, QuerySet): serializer_class = self.get_polymorphic_serializer_for_instance(self.instance) return serializer_class(self.instance, context=self.context).get_fields()