Create Nested Serialier Multi Objects in Django Rest Framework?

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:
It prints: {u'user': [u'2']}>
. No photos POST?
{u'user': [u'2']}>
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.
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