TypeError:perform_create()接受1个位置参数,但给出了2个

时间:2018-08-29 16:17:25

标签: django django-rest-framework

我在这里使用Django Rest Framework。当我使用React从前端发出发布请求时,会返回标题错误。

我的perform_create()函数在我的视图类中:

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)

端点数据格式应为:

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

表格发送:

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

perform_create()函数的目的是从后端提供用户密钥。

我们将不胜感激

1 个答案:

答案 0 :(得分:2)

还添加serializer作为perform_create方法的参数

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

official docs

中了解有关一般视图的更多信息