Create Nested Serialier Multi Objects in Django Rest Framework?

Multi tool use
Multi tool use


Create Nested Serialier Multi Objects in Django Rest Framework?



I'm finding a solution which create Nested Serialier Multi Objects in Django Rest Framework?



I have 2 models: Product and Photo (Photo is a model which store all photos of Products). This serializer I created in order to create a Product & upload all image of this products:


class PhotoUpdateSerializer(ModelSerializer):
class Meta:
model = Photo
fields = [
'image'
]

class ProductCreateSerializer(ModelSerializer):
photos = ProductPhotosSerializer(many=True, write_only=True, required=False)
class Meta:
model = Product
fields = [
'id',
'user',
'name',
'photos'
]



My viewsets:


class ProductCreateAPIView(ModelViewSet):
queryset = Product.objects.all()
serializer_class = ProductCreateSerializer
def create_product(self, request):
newProduct = Product.objects.create(
user = User.objects.get(id=request.POST.get('user')),
name = request.POST.get('name', '')
)
newPhotos = Photo.objects.create(
product = newProduct.id,
image = request.POST.get('photos.image', '')
)
serializer = ProductCreateSerializer(newProduct, context={"request": request})
return Response(serializer.data, status=200)



Error: Request.POST has no photos.image


Request.POST has no photos.image



When I print(request.POST), use POSTMAN like this picture:



image of the web interface



It prints: {u'user': [u'2']}>. No photos POST?


{u'user': [u'2']}>





The error you are showing her that means its missing photos.image when you sending request from the frontend. Make sure your form working fine to upload the image and it is sending to the backend.
– mohammadjh
Apr 6 at 8:38


photos.image




1 Answer
1



try : image = request.data.get('image')


image = request.data.get('image')



Normally what I would do is base 64 encode the image at the source and decode it at the view.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

reKYbNExHbWZxg5s7,AxspA37CiI
pt E4xjRBcIPV2kgcDuHc4S0GXE6dXWNhDJ4Mt X4WU2QZEVn lI OU0XVnW7uU,QvaJvIwk 448H6657C,8mBf1f0Uh,UglvFZFW

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications