我正在研究Django REST框架及其序列化程序,并且在覆盖Create方法时遇到错误.
串行器
class AdddTestModel(串行器s.Serializer):
name = 串行器s.CharField()
score = 串行器s.FloatField()
score2 = 串行器s.FloatField()
song = 串行器s.IntegerField()
def create(self, validated_data):
print(validated_data["song"])
song = Song.objects.get(id=validated_data["song"])
print(song)
instance = TestModel.objects.create(name=validated_data["name"], score = validated_data["score"], score2 = validated_data["score2"], song = song)
return instance
def to_representation(self, instance):
representation = super().to_representation(instance)
return representation
观
@api_view(["POST"])
def addTestModel(request):
串行器 = AdddTestModel(data=request.data)
if 串行器.is_valid():
串行器.save()
print(串行器.data)
return Response("hello", status=status.HTTP_201_CREATED)
return Response("Error", status=status.HTTP_401_UNAUTHORIZED)
I had everything working correctly when I was using a modelSerializer, but wanted to experiment with 串行器.Serializer.
我知道当TestModel对象被持久化到我的数据库中时,Create方法正在工作.
错误似乎来自TO_表示方法,具体地说
representation = super().to_representation(instance)
我收到的错误如下所示
Internal Server Error: /api/addTest/
Traceback (most recent call last):
File "C:\Users\thoma\Documents\venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "C:\Users\thoma\Documents\venv\lib\site-packages\django\core\handlers\base.py", line 197, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\thoma\Documents\venv\lib\site-packages\django\views\decorators\csrf.py", line 56, in wrapper_view
return view_func(*args, **kwargs)
File "C:\Users\thoma\Documents\venv\lib\site-packages\django\views\generic\base.py", line 104, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\thoma\Documents\venv\lib\site-packages\rest_framework\views.py", line 509, in dispatch
response = self.handle_exception(exc)
File "C:\Users\thoma\Documents\venv\lib\site-packages\rest_framework\views.py", line 469, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\thoma\Documents\venv\lib\site-packages\rest_framework\views.py", line 480, in raise_uncaught_exception
raise exc
File "C:\Users\thoma\Documents\venv\lib\site-packages\rest_framework\views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\thoma\Documents\venv\lib\site-packages\rest_framework\decorators.py", line 50, in handler
return func(*args, **kwargs)
File "C:\Users\thoma\Documents\realestate\theFirst观\api\views.py", line 53, in addTestModel
print(串行器.data)
File "C:\Users\thoma\Documents\venv\lib\site-packages\rest_framework\串行器s.py", line 555, in data
ret = super().data
File "C:\Users\thoma\Documents\venv\lib\site-packages\rest_framework\串行器s.py", line 253, in data
self._data = self.to_representation(self.instance)
File "C:\Users\thoma\Documents\venv\lib\site-packages\rest_framework\串行器s.py", line 522, in to_representation
ret[field.field_name] = field.to_representation(attribute)
File "C:\Users\thoma\Documents\venv\lib\site-packages\rest_framework\fields.py", line 915, in to_representation
return int(value)
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'Song'
I know using 串行器s.ModelSerializer makes more sense here, but i'm just wondering if there is any specific reason why this is working with 串行器s.ModelSerializer but not 串行器s.串行器