gpt4 book ai didi

python - views.py 中的 .post() 、 .create() 和 perform_create() 与 serializers.py 中的 .create() 有什么区别

转载 作者:太空狗 更新时间:2023-10-30 00:16:27 35 4
gpt4 key购买 nike

我对 .post() 感到困惑和 .create()perform_create()views.py.create()serializers.py
好像是.create()serializers.py得到 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)

进程会去.post()views.py --> .create()serializers.py --> 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 ")

进程是.create()views.py --> .perform_create()views.py --> .create()serializers.py --> Response给客户

两者都可以!
而且我发现如果我发布无效数据,该过程将不会转到 .perform_create()部分甚至我评论 .create()部分,所以看起来像 .perform_create()无法检查 serializer.is_valid() , 都不是

那么 .post() 之间有什么区别?和 .create()views.py ,我应该只使用 .post() 吗?或者我应该使用 .create()加上 .perform_create()
我想知道,因为我必须使用像 {'custome error!!!!!!!!!'} 这样的客户响应消息,但我对它们的效果感到困惑,我应该把逻辑放在哪里

最佳答案

来自关于 perform_create 钩子(Hook)和其他钩子(Hook)的文档:

These hooks are particularly useful for setting attributes that are implicit in the request, but are not part of the request data. For instance, you might set an attribute on the object based on the request user, or based on a URL keyword argument.

关于python - views.py 中的 .post() 、 .create() 和 perform_create() 与 serializers.py 中的 .create() 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540433/

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