Integration with django-polymorphic? #365
-
Hi there! I'm in the process of experimenting with some options for creating a Django based API, and I'm wondering if Django Ninja has any kind of integration with Django Polymorphic? I've used Graphene (and Graphene Django) for a GraphQL based API in the past, and that integrates well with polymorphic models via the GraphQL "interface" standard. I can see that OpenAPI supports composed types and "oneOf" and "anyOf" keywords; can Django Ninja handle this already? And if not, are there plans to do so? Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Not sure I completely understand your goals but @api.post('/foo')
def some_foo(request, payload: Union[Foo, Bar, Test]):
... |
Beta Was this translation helpful? Give feedback.
-
OpenAPI forces you to describe all fields that you willing to output - so you would need to create union of all child models: something like this: class ManagerSchema(ModelSchema):
class Config:
model = Manager
model_fields = '__all__'
class EmployeeSchema(ModelSchema):
class Config:
model = Employee
model_fields = '__all__'
@api.get('/people', response=List[Union[ManagerSchema, EmployeeSchema]]) # !!!
def list_people(request):
return Person.objects.all() let me know if this works |
Beta Was this translation helpful? Give feedback.
@furious-luke
OpenAPI forces you to describe all fields that you willing to output - so you would need to create union of all child models:
something like this:
let me know if this works