我不是很熟悉Django REST框架,并且已经尝试了很多东西但是无法使我的PATCH请求工作.
我有一个模型序列化器.这与我用来添加新条目的那个相同,理想情况下我想在更新条目时重复使用.
class TimeSerializer(serializers.ModelSerializer):
class Meta:
model = TimeEntry
fields = ('id', 'project', 'amount', 'description', 'date')
def __init__(self, user, *args, **kwargs):
# Don't pass the 'fields' arg up to the superclass
super(TimeSerializer, self).__init__(*args, **kwargs)
self.user = user
def validate_project(self, attrs, source):
"""
Check that the project is correct
"""
.....
def validate_amount(self, attrs, source):
"""
Check the amount in valid
"""
.....
Run Code Online (Sandbox Code Playgroud)
我试图使用基于类的视图:
class UserViewSet(generics.UpdateAPIView):
"""
API endpoint that allows timeentries to be edited.
"""
queryset = TimeEntry.objects.all()
serializer_class …Run Code Online (Sandbox Code Playgroud)