Django Server Error integrating Newsletter signup

Issue

I am having an issue integrating my mailchimp signup to my Django server. I am using Django as a server, then connecting it to my frontend with react native. When going to this url (http://127.0.0.1:8000/subscribe_email) I get the following error:

AssertionError at /subscribe_email
'MailSubscriptionAPIView' should either include a `serializer_class` attribute, or override the `get_serializer_class()` method.

What am I doing wrong? I just want to be able to use the post method to subscribe a user.

Here’s my views.py

# Mailchimp Settings
MAILCHIMP_API_KEY = settings.MAILCHIMP_API_KEY
MAILCHIMP_DATA_CENTER = settings.MAILCHIMP_DATA_CENTER
MAILCHIMP_LIST_ID = settings.MAILCHIMP_EMAIL_LIST_ID


class MailSubscriptionAPIView(GenericAPIView):

    def subscribe_email(email):

        mailchimp = Client()
        mailchimp.set_config({
            "api_key": MAILCHIMP_API_KEY,
            "server": MAILCHIMP_DATA_CENTER
        })
        member_info = {
            "email_address": email,
            "status": "subscribed",
        }
        try:
            mailchimp.lists.add_list_member(MAILCHIMP_LIST_ID, member_info)
        except ApiClientError as error:
            print(error.text)

    def post(self, request, *args, **kwargs):
        email = request.data['email']
        MailSubscriptionAPIView.subscribe_email(email)
        return Response({
                "status_code": status.HTTP_200_OK,
                "message": "Mail added to mailchimp"
            })

urls.py

urlpatterns = [
    path('', include(router.urls)),
    path('subscribe_email', MailSubscriptionAPIView.as_view(), name= 'subscribe-email' ),
]

Any help is appreciated!

Solution

While creating a GenricAPIView, you should provide a serializer class.
Serializer can be used in various ways inside GenricAPIView, one simple example is:

class MailSubscriptionAPIView(GenericAPIView):
    # Here, MailSerializer is a made-up name
    # You should create a serializer class that serializes
    # the model or some custom fields that you need
    serializer_class = MailSerializer

    def subscribe_email(email):

        mailchimp = Client()
        mailchimp.set_config({
            "api_key": MAILCHIMP_API_KEY,
            "server": MAILCHIMP_DATA_CENTER
        })
        member_info = {
            "email_address": email,
            "status": "subscribed",
        }
        try:
            mailchimp.lists.add_list_member(MAILCHIMP_LIST_ID, member_info)
        except ApiClientError as error:
            print(error.text)

    def post(self, request, *args, **kwargs):
        email = request.data['email']
        MailSubscriptionAPIView.subscribe_email(email)
        return Response({
                "status_code": status.HTTP_200_OK,
                "message": "Mail added to mailchimp"
            })

Considering that you have a model called Mail, Serializer class should be something like this:

from rest_framework.serializers import ModelSerializer

# Considering that you have a model called Mail

class MailSerializer(ModelSerializer):
    class Meta:
        model = Mail
        fields = "__all__"

Answered By – Suyog Adhikari

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published