我对
似乎
以下是
.post()
、.create()
和perform_create()
在views.py
中以及在serializers.py
中的.create()
感到困惑。似乎
serializers.py
中的.create()
获取了validated_data
,因此无法检查serializer.is_valid
。以下是
serializers.py
的内容:class PostListSerializer(ModelSerializer):
class Meta:
model = Post
fields = [
'title',
'content',
'publish',
]
def create(self, validated_data):
print("-------------------")
print("5555555 - create", validated_data)
return Post.objects.create(**validated_data)
如果我的代码是:
views.py
class TestPostListAPIView(ListCreateAPIView):
queryset = Post.objects.all()
serializer_class = PostListSerializer
def post(self, request, *args, **kwargs):
serializer = PostListSerializer(data=request.data)
if not serializer.is_valid(raise_exception=False):
# deal with serializer.errors
return Response({'[post]custome error 1 !!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
else:
print("[post]- serializer.validated_data ", serializer.validated_data)
serializer.save()
return Response({'[post]Create success'}, status=status.HTTP_201_CREATED)
这个流程将在views.py
中使用.post()
,然后传递到serializers.py
中的.create()
,最终通过Response
返回给客户端。
如果我的views.py
使用:
class TestPostListAPIView(ListCreateAPIView):
queryset = Post.objects.all()
serializer_class = PostListSerializer
# def post(self, request, *args, **kwargs):
# serializer = PostListSerializer(data=request.data)
# if not serializer.is_valid(raise_exception=False):
# # deal with serializer.errors
# return Response({'[post]custome error 1 !!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
# else:
# print("[post]- serializer.validated_data ", serializer.validated_data)
# serializer.save()
# return Response({'[post]Create success'}, status=status.HTTP_201_CREATED)
def create(self, request, *args, **kwargs):
serializer = self.get_serializer(data=request.data)
print("44444-1 perform_create ", serializer)
if not serializer.is_valid(raise_exception=False):
print("44444-3 serializer.is_valid() ", serializer.is_valid())
return Response({'custome error!!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
else:
print("44444-7 serializer.validated_data ", serializer.validated_data)
self.perform_create(serializer)
return Response({ 'Create success'}, status=status.HTTP_201_CREATED)
def perform_create(self, serializer):
print("1111-1 perform_create ", serializer)
if not serializer.is_valid(raise_exception=False):
print("1111-3 serializer.is_valid() ", serializer.is_valid())
return Response({'custome error!!!!!!!!!'}, status=status.HTTP_400_BAD_REQUEST)
else:
print("1111-7 serializer.validated_data ", serializer.validated_data)
user = User.objects.filter(id=8)
serializer.save(user=user.first())
print("1111-8 perform_create ")
这个过程是在views.py
中使用.create()
-->.perform_create()
-->.create()
在serializers.py
中-->向客户端发送Response
两种方式都可以工作!
我发现如果我发布无效数据,即使我注释了.create()
部分,该过程也不会进入.perform_create()
部分,因此似乎.perform_create()
不能检查serializer.is_valid()
,也不能检查该方法。
那么,在views.py
中,.post()
和.create()
有什么区别?我应该只使用.post()
还是应该使用.create()
加上.perform_create()
?
我想知道,因为我必须使用自定义响应消息,例如{'custom error!!!!!!!!!'}
,但我对其影响感到困惑,我应该把逻辑放在哪里。