本文整理匯總了Python中rest_framework.tests.models.ReadOnlyManyToManyModel.save方法的典型用法代碼示例。如果您正苦於以下問題:Python ReadOnlyManyToManyModel.save方法的具體用法?Python ReadOnlyManyToManyModel.save怎麽用?Python ReadOnlyManyToManyModel.save使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rest_framework.tests.models.ReadOnlyManyToManyModel
的用法示例。
在下文中一共展示了ReadOnlyManyToManyModel.save方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: ReadOnlyManyToManyTests
# 需要導入模塊: from rest_framework.tests.models import ReadOnlyManyToManyModel [as 別名]
# 或者: from rest_framework.tests.models.ReadOnlyManyToManyModel import save [as 別名]
class ReadOnlyManyToManyTests(TestCase):
def setUp(self):
class ReadOnlyManyToManySerializer(serializers.ModelSerializer):
rel = serializers.RelatedField(many=True, read_only=True)
class Meta:
model = ReadOnlyManyToManyModel
self.serializer_class = ReadOnlyManyToManySerializer
# An anchor instance to use for the relationship
self.anchor = Anchor()
self.anchor.save()
# A model instance with a many to many relationship to the anchor
self.instance = ReadOnlyManyToManyModel()
self.instance.save()
self.instance.rel.add(self.anchor)
# A serialized representation of the model instance
self.data = {'rel': [self.anchor.id], 'id': 1, 'text': 'anchor'}
def test_update(self):
"""
Attempt to update an instance of a model with a ManyToMany
relationship. Not updated due to read_only=True
"""
new_anchor = Anchor()
new_anchor.save()
data = {'rel': [self.anchor.id, new_anchor.id]}
serializer = self.serializer_class(self.instance, data=data)
self.assertEqual(serializer.is_valid(), True)
instance = serializer.save()
self.assertEqual(len(ReadOnlyManyToManyModel.objects.all()), 1)
self.assertEqual(instance.pk, 1)
# rel is still as original (1 entry)
self.assertEqual(list(instance.rel.all()), [self.anchor])
def test_update_without_relationship(self):
"""
Attempt to update an instance of a model where many to ManyToMany
relationship is not supplied. Not updated due to read_only=True
"""
new_anchor = Anchor()
new_anchor.save()
data = {}
serializer = self.serializer_class(self.instance, data=data)
self.assertEqual(serializer.is_valid(), True)
instance = serializer.save()
self.assertEqual(len(ReadOnlyManyToManyModel.objects.all()), 1)
self.assertEqual(instance.pk, 1)
# rel is still as original (1 entry)
self.assertEqual(list(instance.rel.all()), [self.anchor])