Я получаю следующую ошибку при публикации нового продукта в моем приложении
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
my serializer.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": []
}]
}