How to implement polymorphic serializer with non-ORM entities? #1034
SafaAlfulaij
started this conversation in
General
Replies: 1 comment 2 replies
-
IF the model looks like class Cloth(models.Model):
cloth_type = models.JSONField() Then your serializer could looks like class ClothSerializer(serilizers.ModelSerializer):
cloth_type = SerializerMethodField()
class Meta:
model = Cloth
def get_cloth_type(self, obj):
# handles cloth type serializing in |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
My use case is that I am using a
JSONField
database column to store 10s types of data in nested arrays and objects.Array children should have the same type, but that type can have a field related to different serializers.
Example (very simplified and abstracted):
WardrobeSerializer
:"xxx"
"wordrobe"
cloths
:ClothSerializer(many=True)
ClothSerializer
:"xxx"
"cloth"
clothType
:"tShirt"
or"pant"
,designer
:"Mr. Designer"
, etc...details
: (polymorphic)TShirtSerializer
orPantSerializer
TShirtSerializer
:"xxx"
"tShirt"
size
:"L"
,color
:"black"
, etc...PantSerializer
:"xxx"
"pant"
waist
:22
,pantType
:"slim"
, etc...Example of data:
Beta Was this translation helpful? Give feedback.
All reactions