0

I have two models.

class Order(..):
   ...

class OrderImage(..):
   order = ForeignKey('Order...)

And I have a form with dropzone.js.

This form returns data suitable for creating an order using just ViewSet but it obviously doesn't create OrderImage objects, even there are image[0], image[1] etc. data in the POST request.

What's the best way to create OrderImages alongside with the Order in DRF?

  1. modify dropzone output (didn't find the way)
  2. Modify request.data inside ViewSet (how?)
  3. Modify OrderSerializer to create OrderImage objects?

How would you do that?

EDIT

class SubOfferImageSerializer(serializers.ModelSerializer):
    class Meta:
        model = SubOfferImage
        fields = ['file']


class SubOfferSerializer(serializers.ModelSerializer):
    images = SubOfferImageSerializer(many=True, required=False)
    client_status_display = serializers.CharField(source='get_client_status_display', read_only=True)
    system_status_display = serializers.CharField(source='get_system_status_display', read_only=True)

    class Meta:
        model = SubOffer
        fields = [field.name for field in model._meta.fields] + ['client_approve_url',
                                                                 'system_decline_url',
                                                                 'client_status_display',
                                                                 'system_status_display','images']

Now the problem is that I have raw images in the data, not serialized SubOrderImage objects.

I'm trying to change the input overriding perform_create but it doesn't seem to be a best option. The better would be to do that inside serializer.

Milano
  • 18,048
  • 37
  • 153
  • 353
  • IMHO, Modifying corresponding serializer (`OrderSerializer` ) would be nice – JPG Dec 12 '19 at 15:56
  • @JPG I've added images = OrderImageSerializer(many=True...) into the serializer but I need to somehow modify the json. – Milano Dec 12 '19 at 16:01
  • Can you add the pieces of information regarding the attempt you have tried with DRF nested serializer? – JPG Dec 12 '19 at 16:22
  • @JPG Done, It's at the bottom of the question. I'm trying to modify ViewSet.perform_create but it doesn't make sense.. it should work for updating too and I would rather do that inside the serializer. – Milano Dec 12 '19 at 16:25
  • It seems you are trying to upload image/file...is that right? – JPG Dec 12 '19 at 16:27
  • Yes I do. The main problem is with json format I think. Files are in binary format. – Milano Dec 12 '19 at 16:28
  • The very first valuable point is, ***You can't send image/image data using `application/json` formate*** – JPG Dec 12 '19 at 16:30
  • Anyway, Does this answer your question, [**Django REST: Uploading and serializing multiple images**](https://stackoverflow.com/questions/48756249/django-rest-uploading-and-serializing-multiple-images/48762785#48762785) – JPG Dec 12 '19 at 16:31
  • DRF has [`FormParser`](https://www.django-rest-framework.org/api-guide/parsers/#formparser) which _can_ allow sending images as well as model data. For writing the structures in a nested fashion, I recommend looking at [`drf_writable_nested` package](https://github.com/beda-software/drf-writable-nested) . Consider also [Django REST Framework Bulk](https://github.com/miki725/django-rest-framework-bulk). Lastly, you can send binary data in json if you b64 encode it on the client and then b64 decode it in your DRF endpoint. – Ross Rogers Dec 12 '19 at 16:46

0 Answers0