Creating multi-tenant objects in the Django Rest Framework?

Am I finding a solution that creates Nested Serialier Multi Objects in the Django Rest Framework?

I have 2 models: Product and Photo (Photo is a model in which all photos of Products are stored). This serializer that I created to create the Product and download the entire image of this product:

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 views:

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

When I type (request.POST), use POSTMAN as shown in the picture:

enter image description here

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

How can i fix this? Please help me because I lost 2 days for this :(

+4
source share
1 answer

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

, , base 64 .

0

Source: https://habr.com/ru/post/1695849/


All Articles