gpt4 book ai didi

python - 对象不可迭代,Django REST Framework

转载 作者:行者123 更新时间:2023-12-01 08:42:37 24 4
gpt4 key购买 nike

将新产品发布到我的应用程序时出现以下错误

Traceback (most recent call last):
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/viewsets.py", line 116, in view
return self.dispatch(request, *args, **kwargs)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/mixins.py", line 20, in create
serializer.is_valid(raise_exception=True)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 236, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 434, in run_validation
value = self.to_internal_value(data)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 488, in to_internal_value
validated_value = field.run_validation(primitive_value)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 623, in run_validation
value = self.to_internal_value(data)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 662, in to_internal_value
validated = self.child.run_validation(item)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 436, in run_validation
self.run_validators(value)
File "/mnt/c/Users/connect/Desktop/PIM/env/lib/python3.6/site-packages/rest_framework/serializers.py", line 465, in run_validators
to_validate.update(value)
TypeError: 'Category' object is not iterable

我的序列化器.py

from rest_framework import serializers
from products_and_categories.models import Product, Category
from django.db import models

class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ('id', "name", 'products', 'categories')

def to_representation(self, obj):
if 'categories' not in self.fields:
self.fields['categories'] = CategorySerializer(obj, many=True)
return super(CategorySerializer, self).to_representation(obj)

def to_internal_value(self, data):
try:
try:
obj_id = data['id']
return Category.objects.get(id=obj_id)
except KeyError:
raise serializers.ValidationError(
'id is a required field.'
)
except ValueError:
raise serializers.ValidationError(
'id must be an integer.'
)
except Category.DoesNotExist:
raise serializers.ValidationError(
'Obj does not exist.'
)


class ProductSerializer(serializers.ModelSerializer):
categories = CategorySerializer(many=True)#, queryset= Category.objects.all(), slug_field='categories')

class Meta:
model = Product
fields = ('id', "product_code", "name", "quantity", "price", 'categories')
#.....

知道为什么会发生这种情况吗?

我不太明白它不可迭代是什么意思,代码的哪一部分到底导致了这个错误?

我真的很感激任何帮助

帖子采用以下格式

{

"id": 22,

"product_code": "dklj",

"name": "kdlj",

"quantity": 15,
"price": 15,
"categories": [ {
"id": 5,
"name": "Test category 3",
"products": [],
"categories": []
}]

}

最佳答案

这里:self.fields['categories'] = CategorySerializer(obj, Many=True)

您告诉 CategorySerializer 准备一个类别列表,但您只传递了一个类别。

关于python - <myObject> 对象不可迭代,Django REST Framework,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53438138/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com