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 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 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()