本文整理汇总了Python中rest_framework.mixins.UpdateModelMixin方法的典型用法代码示例。如果您正苦于以下问题:Python mixins.UpdateModelMixin方法的具体用法?Python mixins.UpdateModelMixin怎么用?Python mixins.UpdateModelMixin使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rest_framework.mixins
的用法示例。
在下文中一共展示了mixins.UpdateModelMixin方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: update
# 需要导入模块: from rest_framework import mixins [as 别名]
# 或者: from rest_framework.mixins import UpdateModelMixin [as 别名]
def update(self, request, *args, **kwargs):
"""Update a resource."""
# NOTE: Use the original method instead when support for locking is added:
# https://github.com/encode/django-rest-framework/issues/4675
# return super().update(request, *args, **kwargs)
with transaction.atomic():
return self._update(request, *args, **kwargs)
# NOTE: This is a copy of rest_framework.mixins.UpdateModelMixin.update().
# The self.get_object() was replaced with the
# self.get_object_with_lock() to lock the object while updating.
# Use the original method when suport for locking is added:
# https://github.com/encode/django-rest-framework/issues/4675
示例2: setUp
# 需要导入模块: from rest_framework import mixins [as 别名]
# 或者: from rest_framework.mixins import UpdateModelMixin [as 别名]
def setUp(self):
class DummySerializer(serializers.Serializer):
nickname = serializers.CharField()
color = serializers.CharField()
def create(self, validated_data):
return mock.Mock(pk=2, **validated_data)
def update(self, obj, validated_data):
return mock.Mock(pk=obj.pk, **validated_data)
class GetDogMixin(object):
def get_object(self):
return mock.Mock(pk=1, nickname='Spot', color='black')
class DummyView(viewsets.NotificationMixin,
mixins.CreateModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
GetDogMixin,
GenericViewSet):
serializer_class = DummySerializer
mapping = {
'post': 'create',
'put': 'update',
'delete': 'destroy',
}
self.view = DummyView.as_view(mapping, basename='dummy')