Home > Net >  DRF .as_viewset on {'post': 'list'} return attribute error?
DRF .as_viewset on {'post': 'list'} return attribute error?

Time:01-03

I am trying to send some text: example: "Hello World" to DRF end-point.

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:

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'

To test this out:

urls.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

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

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

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__'

CodePudding user response:

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:

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

    def list(
        self, request, *args, **kwargs
    ):  #            
  • Related