我在这里使用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()函数的目的是从后端提供用户密钥。
我们将不胜感激
答案 0 :(得分:2)
还添加serializer
作为perform_create
方法的参数
def perform_create(self, serializer):
user = self.request.user
serializer.save(user=user)
中了解有关一般视图的更多信息