Zeph Terence Sibley
Zeph Terence Sibley

Reputation: 23

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

I am using Django Rest Framework here. When I make a post request from the front end with React, the title error is returned.

My perform_create() function is within my view class:

class MaxListCreate(generics.ListCreateAPIView):
  queryset = Max.objects.all()
  serializer_class = MaxSerializer
  filter_backends = (filters.OrderingFilter,)
  ordering_fields = ('exercise', 'date',)
  ordering = ('exercise', 'date',)
  permission_classes = (permissions.IsAuthenticated,)

  def perform_create(self):
    user = self.request.user
    serializer.save(user=user)

  # User can only access the data associated with the user.
  def get_queryset(self):
    user = self.request.user
    return Max.objects.filter(user=user)

The endpoint data format should be:

{
    "id": 5,
    "date": "2018-08-07",
    "max_lift": 80,
    "user": 1,
    "exercise": 1
},

and the form sends:

{"exercise":"3","date":"2018-08-23","max_lift":"70"}

The intention of the perform_create() function is to supply the user key from the back end.

Any help would be appreciated,

Upvotes: 0

Views: 1086

Answers (1)

Satendra
Satendra

Reputation: 6865

Add serializer also, as parameter of perform_create method

def perform_create(self, serializer):
    user = self.request.user
    serializer.save(user=user)

Read more about generic-views in official docs

Upvotes: 3

Related Questions