djangodjango-modelsdjango-rest-frameworkdjango-polymorphic

Cannot create a polymorphic project instance in Django Rest framework JSON API example project


I am trying to understand how polymorphic resources work in django-rest-framework-json-api and I get the following error both on my own project and the example project provided by django-rest-framework-json-api. My models are created using django-polymorphic. The example project I am trying to run is https://github.com/django-json-api/django-rest-framework-json-api/tree/master/example.

Endpoint : localhost:8000/projects

{
"errors": [
{
"detail": "Incorrect relation type. Expected on of [artProject, researchProject], received None.",
"source": {
"pointer": "/data"
},
"status": "409"
}
]
}

Project page's form used to create a new instance doesn't have any field that I can specify the type of the Project. It only shows the topic field. So I am also wondering how I can create an Art project for instance.

In the documentation it says the type is determined by the resource_name in Meta data of either view, serializer or model. But type is none in the returned json data. It is because serializer.instance = None which is used to determine the type when force_type_resolution is set to True. It is set to True to force polymorphic serializers to resolve the type based on instance.

So I can't seem to fix this issue, maybe I am missing some configuration or there really is a bug in the code but I have no idea. Can someone please tell me how to solve this if you have come across this issue or know how to get the example app working with polymorphic resources?


Solution

  • I was initially trying to create instances in the browser which threw the error above and didn't let me choose child types. I tested this on Postman and specified the type and it worked. I had the impression that I could even create an instance of Project as well (Parent instance itself, not a child instance) but as error message clearly says, I can't.