lee*_*ngo -1 python django django-rest-framework
我在 DRF 中进行自定义,以便在正文或标题中输入空值时给出错误响应值。然而,挤压代码后,测试结果出现以下错误。
\n\n\nAttributeError:“set”对象没有属性“items”
\n
我不知道这个错误意味着什么,也不知道为什么会发生。你能告诉我我的代码有什么问题吗?这是我的代码。
\n视图.py
\nclass CreatePostView (ModelViewSet) :\n serializer_class = PostSerializer\n permission_classes = [IsAuthenticated]\n queryset = Post.objects.all()\nRun Code Online (Sandbox Code Playgroud)\n序列化器.py
\nclass PostSerializer (serializers.ModelSerializer) :\n author = serializers.CharField(source='author.username', read_only=True)\n title = serializers.CharField(allow_null=True)\n text = serializers.CharField(allow_null=True)\n image = ImageSerializer(many=True)\n\n class Meta:\n model = Post\n fields = ['pk', 'author', 'title', 'text', 'like', 'liker', 'image', 'view']\n\n def validate (self, attrs) :\n title = attrs.get('title', '')\n text = attrs.get('text', '')\n\n error = {}\n\n if title is None and text is None :\n error['message'] = '\xec\xa0\x9c\xeb\xaa\xa9\xea\xb3\xbc \xeb\xb3\xb8\xeb\xac\xb8\xec\x9d\x84 \xeb\x84\xa3\xec\x96\xb4\xec\xa3\xbc\xec\x84\xb8\xec\x9a\x94.'\n raise serializers.ValidationError(error)\n\n if title is None :\n error['message'] = '\xec\xa0\x9c\xeb\xaa\xa9\xec\x9d\x84 \xeb\x84\xa3\xec\x96\xb4\xec\xa3\xbc\xec\x84\xb8\xec\x9a\x94.'\n raise serializers.ValidationError(error)\n\n if text is None :\n error['message'] = '\xeb\xb3\xb8\xeb\xac\xb8\xec\x9d\x84 \xeb\x84\xa3\xec\x96\xb4\xec\xa3\xbc\xec\x84\xb8\xec\x9a\x94.'\n raise serializers.ValidationError(error)\n\n return attrs\n\n def create (self, validated_data) :\n return Post.objects.create(**validated_data)\nRun Code Online (Sandbox Code Playgroud)\n追溯
\nTraceback (most recent call last):\n File "C:\\Users\\kurak\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\django\\core\\handlers\\exception.py", line 34, in inner\n response = get_response(request)\n File "C:\\Users\\kurak\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\django\\core\\handlers\\base.py", line 115, in _get_response\n response = self.process_exception_by_middleware(e, request)\n File "C:\\Users\\kurak\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\django\\core\\handlers\\base.py", line 113, in _get_response\n response = wrapped_callback(request, *callback_args, **callback_kwargs)\n File "C:\\Users\\kurak\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\django\\views\\decorators\\csrf.py", line 54, in wrapped_view\n return view_func(*args, **kwargs)\n File "C:\\Users\\kurak\\AppData\\Local\\Programs\\Python\\Python38-32\\lib\\site-packages\\rest_framework\\viewsets.py", line 102, in view\n for method, action in actions.items():\nAttributeError: 'set' object has no attribute 'items'\nRun Code Online (Sandbox Code Playgroud)\nurls.py
\nfrom django.urls import path, include\nfrom .views import CreatePostView\nfrom django.conf import settings\nfrom django.conf.urls.static import static\n\nurlpatterns = [\n path('post', CreatePostView.as_view({'post', 'create'})),\n] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
1338 次 |
| 最近记录: |