-
Notifications
You must be signed in to change notification settings - Fork 300
-
I have the following structure of serializers
class A(Serializer) class JSONAPIMeta: resource_name = "A" class B(Serializer) class JSONAPIMeta: resource_name = "B" class MySerializer(Serializer) my_field = OneOfSerializer(A(), B())
OneOfSerializer is my custom serializer accepting values either for A or B. The main problem is that it doesn't contain JSONAPIMeta and DRF-jsonapi raises an exception. Would it be possible to somehow return the resource_name dynamically based on the data, so that the relation is correctly set?
Beta Was this translation helpful? Give feedback.
All reactions
Replies: 1 comment 2 replies
-
What version of DJA are you using? Since DJA version 4.0.0 nested serializers are not rendered as relationships anymore but as attribute value. So when I look at your example this would actually be rendered as attribute value instead and resource name is not needed in this case.
But if you wanna have dynamic relationships you can use SerializerMethodResourceRelatedField and the resource name is then calculated by the returning instance.
Does this help? Or have I misunderstood your question?
Beta Was this translation helpful? Give feedback.
All reactions
-
I'm using version 3.1.0.
Is the SerializerMethodResourceField going to work for serializer which is not tied to a model? I mean, I'm initializing it with custom data not necessarily being taken from django model. I'll try to experiment with that.
Beta Was this translation helpful? Give feedback.
All reactions
-
Yes it should work but give it a try.
Beta Was this translation helpful? Give feedback.