class AnnotationSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Annotation
class ImageSerializer(serializers.HyperlinkedModelSerializer):
annotations = AnnotationSerializer(many=True, required=False)
class Meta:
depth = 1
model = Image
exclude = ('owner‘,)
An annotation has an image foreign key attribute and so images have potentially multiple annotations. I’d like to create an image with nested annotations via a post request to an images endpoint including the list of annotations for this image. Posting my data json encoded to the images endpoint does work and creates an image with appropriate annotations.
But when I try to upload an actual image, I have to use a multipart/form-encoded post request instead of a json one to make fileupload possible. And now I’m having a hard time getting my nested list of image annotations included in this request. Maybe I could put a json encoded string in some form-field and manually parse it in the view, overwriting request.DATA, but this seems to be really ugly.
I wonder whether there’s a better way to achieve what I’m trying to do :).