Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

TypeError: post() takes 1 positional argument but 2 were given

I have my model class:

class Subscription(models.Model):
    email = models.EmailField(max_length=250, unique=True)

    def __str__(self):
        return self.email

and my View:

class SubscriptionView(APIView):
    queryset = Subscription.objects.all()

    def post(request):
        email = request.data.get('email')
        print(email)
        save_email = Subscription.objects.create(email=email)
        save_email.save()

        return Response(status=status.HTTP_201_CREATED)

My model only takes in 1 field which is the email. Not sure why I keep getting ‘TypeError: post() takes 1 positional argument but 2 were given’

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Appreciate any help!

>Solution :

Your post is a method, so the first parameter is self:

class SubscriptionView(APIView):
    queryset = Subscription.objects.all()

    def post(self, request):
        email = request.data.get('email')
        Subscription.objects.create(email=email)
        return Response(status=status.HTTP_201_CREATED)

It is however quite seldom that one implements a post method itself. Usually you work for example with a CreateAPIView [drf-doc], and one can let the serializer, etc. handle all the work.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading