1 回答

TA貢獻(xiàn)1827條經(jīng)驗 獲得超9個贊
你想要的是perform_create方法:
class PlanViewSets(viewsets.ModelViewSet):
"""A viewset for non-admin user see list and admin can curd plan model"""
model = Plan
queryset = Plan.objects.all().order_by('-id')
serializer_class = serializers.PlanSerializer
permission_classes = (IsAuthenticated,)
authentication_classes = (TokenAuthentication,)
def get_queryset(self):
"""retrieve plan just for its own user who is authenticated already"""
return self.queryset.filter(user=self.request.user)
def perform_create(self, serializer):
serializer.save(user=self.request.user)
添加回答
舉報