Home > Blockchain >  django serializer error: images_data = self.context['request'].FILES KeyError: 'reque
django serializer error: images_data = self.context['request'].FILES KeyError: 'reque

Time:12-02

models.py

# 
from django.db import models
from user.models import User
from chat.models import TradeChatRoom, AuctionChatRoom



class Goods(models.Model):
    class Meta:
        db_table = 'Goods'
        ordering = ['-created_at'] # 일단 추가해뒀습니다

    seller = models.ForeignKey(User, on_delete=models.CASCADE, related_name='sell_goods')
    buyer = models.ForeignKey(User, on_delete=models.CASCADE, related_name='buy_goods', null=True)
    trade_room = models.ForeignKey(TradeChatRoom, on_delete=models.CASCADE)
    auction_room = models.ForeignKey(AuctionChatRoom, on_delete=models.CASCADE)

    title = models.CharField(max_length=256)
    content = models.TextField()
    category = models.CharField(max_length=32)
    status = models.BooleanField(null=True)
    predict_price = models.IntegerField()
    start_price = models.IntegerField()
    high_price = models.IntegerField(null=True)
    start_date = models.DateField(null = True)
    start_time = models.DateTimeField(null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    
    like = models.ManyToManyField(User, related_name='like_goods', null=True)



class GoodsImage(models.Model):
    class Meta:
        db_table = "GoodsImage"
    goods = models.ForeignKey(Goods, on_delete=models.CASCADE)
    image = models.ImageField(upload_to='goods/')

serializer.py

from rest_framework import serializers from .models import Goods,GoodsImage

class GoodImageSerializer(serializers.ModelSerializer):
    image = serializers.ImageField(use_url=True)

    def get_image(self, obj):
        image = obj.goods_set.all()
        return GoodsPostSerializer(instance=image, many = True, context = self.context)
    
    class Meta:
        model = GoodsImage
        field =('image',)


class GoodsPostSerializer(serializers.ModelSerializer):
    image = GoodImageSerializer(many=True, read_only = True)

    class Meta:
        model = Goods
        fields = (
        'seller', 'buyer','auction_room','title','content',
        'category','status','predict_price','start_price','high_price',
        'trade_room','start_date','start_time','created_at','like','image',
        )
        read_only_fields = ("seller",)

    def create(self, validated_data):
        goods = Goods.objects.create(**validated_data)
        images_data = self.context['request'].FILES
        for image_date in images_data.getlist('image'):
            GoodsImage.objects.create(goods = goods, image = image_date)
        return goods

error

images_data = self.context['request'].FILES KeyError: 'request'

I want to save multiple images, but I keep getting an error. I don't know what to do anymore. I searched for a method and followed it, but it seems that I am the only one who gets an error. Please help if you know how to solve this problem. And I want to know if it is correct to put it in a list like "image":["12.jpeg,"13.jpeg] when inserting multiple images through postman.

It's hard not being able to solve this problem. please help me if you know the answer

CodePudding user response:

Change GoodImageSerializer calling this:

GoodImageSerializer(instance=images, many = True, context={'request': request})

Then change your GoodsPostSerializer's create method like this:

def get_image(self, obj):
    image = obj.goods_set.all()
    request = self.context['request']
    return GoodsPostSerializer(instance=image, many = True, context={'request': request})
  • Related