1

I am working on a project in which I am using django-polymorphic for retrieving polymorphic result sets. I have some models which inherit from a parent model. For example, Model B inherits from Model A and Model C also inherits from Model A. Model B and Model C have their own serializers and when I query all records for model A, I get a mixed resultset containing instance of Model B and C.

How can I dynamically select serializer based on the instance?

Thanks

4

1 回答 1

0

查看Django REST 多态。它是 Django REST Framework 与 Django Polymorphic 的集成。它完全符合您的要求。

您需要做的就是为父模型的每个子模型创建序列化器,创建PolymorphicSerializer包含每个模型到相应序列化器的映射,并PolymorphicSerializer在需要时使用它。

于 2018-03-14T16:07:04.640 回答