简体   繁体   中英

TypeError: Field 'id' expected a number but got OrderedDict

I want to post request django rest but this error.I have two Serializer.I want to post a request for full data not via id

Exception Value:    
Field 'id' expected a number but got OrderedDict([('title', 'id:2 Смартфон Samsung Galaxy Note 20 Ultra 256GB Black'), ('price', 2019)]).  



 class ShopSerializer(serializers.ModelSerializer):
        img1 = serializers.SerializerMethodField('get_image_url')
        img2 = serializers.SerializerMethodField('get_image_url')
        img3 = serializers.SerializerMethodField('get_image_url')
        img4 = serializers.SerializerMethodField('get_image_url')
        img5 = serializers.SerializerMethodField('get_image_url')
        img6 = serializers.SerializerMethodField('get_image_url')
        class Meta:
            model = smartphone
            fields = ("id", "img1", "img2", "img3", "img4", "img5", "img6","title","price","slug")
        def get_image_url(self, obj):
            return obj.image.url
        
        
  class OrderCreateSerializer(serializers.ModelSerializer):
        smartphone=ShopSerializer(many=True)
        class Meta:
            model = order
            fields = '__all__'
    
        def create(self, validated_data):
            
            smartphone = validated_data.pop('smartphone')
            question = order.objects.create(**validated_data)
            question.smartphone.set(smartphone)
            return question

how to solve this problem how to solve this problem how to solve this problem how to solve this problem

views

class OrderCreateView(generics.CreateAPIView):
    queryset = order.objects.all()
    serializer_class = OrderCreateSerializer

Please try the following post request:

{
    "smartphone": [{
        "id": 2,
        "img1": "/media/photos/498f6387e90cfee88f98c4379b0d2057ec3623ee_228615_1_qhhauLD.jpg",
        "img2": "/media/photos/aaf2ece82f9b4d357c78c3bac9eb61e4b6a5dc0e_228615_3_K7RDb8Z.jpg",
        "img3": "/media/photos/00d7a263681301aa35d9dad51cf7d0859965bc5c_228615_2_jBLEZqi.jpg",
        "img4": "/media/photos/498f6387e90cfee88f98c4379b0d2057ec3623ee_228615_1_M9GV72N.jpg",
        "img5": "/media/photos/fe49e462ef4847ac6296d4bba6cd4ab35d374daf_228615_5_CgYDohH.jpg",
        "img6": "/media/photos/b036713f6015464a366dbc1890f0b66041015f64_227736_1_1yAxj5T.jpg",
        "price": 2019,

        "slug": "id2-samsung-galaxy-note-20-ultra-256gb-black",
        "title": "id:2 Смартфон Samsung Galaxy Note 20 Ultra 256GB Black"
    }],
    "orderCustomer": 6,
    "orderAddress": 5
}

Update:

Also make sure that this line works as expected question.smartphone.set(smartphone) .

smartphones = validated_data.pop('smartphone')
smartphone_models = []
for sm in smartphones:
    print("value of sm is: {}".format(sm))
    print("type of sm is: {}".format(type(sm)))
    new_smartphone = smartphone.objects.create(sm)
    new_smartphone.save()
    smartphone_models.append(new_smartphone)
question = order.objects.create(**validated_data)
print(smartphone_models)
question.smartphone.set(smartphone_models)
{
    "smartphone": [
"id": 2
"img1": "/media/photos/498f6387e90cfee88f98c4379b0d2057ec3623ee_228615_1_qhhauLD.jpg"
"img2": "/media/photos/aaf2ece82f9b4d357c78c3bac9eb61e4b6a5dc0e_228615_3_K7RDb8Z.jpg"
"img3": "/media/photos/00d7a263681301aa35d9dad51cf7d0859965bc5c_228615_2_jBLEZqi.jpg"
"img4": "/media/photos/498f6387e90cfee88f98c4379b0d2057ec3623ee_228615_1_M9GV72N.jpg"
"img5": "/media/photos/fe49e462ef4847ac6296d4bba6cd4ab35d374daf_228615_5_CgYDohH.jpg"
"img6": "/media/photos/b036713f6015464a366dbc1890f0b66041015f64_227736_1_1yAxj5T.jpg"
"price": 2019

"slug": "id2-samsung-galaxy-note-20-ultra-256gb-black"
"title": "id:2 Смартфон Samsung Galaxy Note 20 Ultra 256GB Black"
],
    "orderCustomer": 6,
    "orderAddress": 5
}

My post request it looks like this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM