简体   繁体   English

{'post': 'list'} 上的 DRF.as_viewset 返回属性错误?

[英]DRF .as_viewset on {'post': 'list'} return attribute error?

I am trying to send some text: example: "Hello World" to DRF end-point.我正在尝试发送一些文本:示例:“Hello World”到 DRF 端点。

This endpoint on receiving this text is to send me a e-mail with the text.接收此文本的端点是向我发送一封包含该文本的电子邮件。

When I hit the end-point with Postman, I get the error:当我以 Postman 到达终点时,出现错误:

Internal Server Error: /api/errors Traceback (most recent call last): File "/Users/sid/eb-virt/lib/python3.8/site-packages/django/core/handlers/exception.py", line 55, in inner response = get_response(request) File "/Users/sid/eb-virt/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/Users/sid/eb-virt/lib/python3.8/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view return view_func(*args, **kwargs) File "/Users/sid/eb-virt/lib/python3.8/site-packages/rest_framework/viewsets.py", line 117, in view handler = getattr(self, action) AttributeError: 'ErrorMsgViewSet' object has no attribute 'list'内部服务器错误:/api/errors Traceback(最近调用最后):文件“/Users/sid/eb-virt/lib/python3.8/site-packages/django/core/handlers/exception.py”,第 55 行, inner response = get_response(request) File "/Users/sid/eb-virt/lib/python3.8/site-packages/django/core/handlers/base.py", line 197, in _get_response response = wrapped_callback( request, *callback_args, **callback_kwargs) 文件“/Users/sid/eb-virt/lib/python3.8/site-packages/django/views/decorators/csrf.py”,第 54 行,在 wrapped_view return view_func(* args, **kwargs) 文件“/Users/sid/eb-virt/lib/python3.8/site-packages/rest_framework/viewsets.py”,第 117 行,视图 handler = getattr(self, action) AttributeError: ' ErrorMsgViewSet' object 没有属性 'list'

To test this out:要对此进行测试:

urls.py网址.py

from django.urls import path
from .api import *

urlpatterns = [
    path('api/errors', ErrorMsgViewSet.as_view({'post': 'list'}), name='errors'),
]

# I tried .as_view(), which gave me an error to change to the above format
# i tried using router.register() and got errors on using generic.GenericAPIview

api.py api.py

from rest_framework import viewsets
from email_alerts.serializers import ErrorsSerializer


class ErrorMsgViewSet(viewsets.GenericViewSet):
    serializer_class = ErrorsSerializer
    permission_classes = [
        
    ]

    def post(self, request, *args, **kwargs):
        print(request.data)

models.py模型.py

from django.db import models


# Create your models here.
class ErrorsModel(models.Model):
    error_msg = models.CharField(max_length=5000, blank=True, null=True)

serializers.py序列化程序.py

from rest_framework import serializers
from email_alerts.models import ErrorsModel


class ErrorsSerializer(serializers.ModelSerializer):
    error_msg = serializers.CharField(
        required=False, allow_null=True, allow_blank=True)

    class Meta:
        model = ErrorsModel
        fields = '__all__'

You made a mapping such that a POST request is mapped to the list view, which is strange , so you need to implement a list method:你做了一个映射,这样一个 POST 请求被映射到list视图,这很奇怪,所以你需要实现一个list方法:

class ErrorMsgViewSet(viewsets.GenericViewSet):
    serializer_class = ErrorsSerializer
    permission_classes = []

    def list(
        self, request, *args, **kwargs
    ):  # 🖘 will be triggered with a POST request
        print(request.data)
        # …

You probably however do not want this mapping, so you register it with:但是你可能想要这个映射,所以你注册它:

urlpatterns = [
    #                 map a POST request to post 🖟
    path('api/errors', ErrorMsgViewSet.as_view({'post': 'post'}), name='errors'),
]

and then thus implement a post method.然后实现一个post方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM