mirror of
				https://github.com/encode/django-rest-framework.git
				synced 2025-11-04 01:47:59 +03:00 
			
		
		
		
	Merge pull request #315 from Roarster/emptymanytomany
#314 Fix for manytomany field being required in the payload even though ...
This commit is contained in:
		
						commit
						5d76f03ac6
					
				| 
						 | 
				
			
			@ -256,6 +256,9 @@ class ManyRelatedMixin(object):
 | 
			
		|||
        return [self.to_native(item) for item in value.all()]
 | 
			
		||||
 | 
			
		||||
    def field_from_native(self, data, field_name, into):
 | 
			
		||||
        if self.readonly:
 | 
			
		||||
            return
 | 
			
		||||
            
 | 
			
		||||
        try:
 | 
			
		||||
            # Form data
 | 
			
		||||
            value = data.getlist(self.source or field_name)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -62,7 +62,12 @@ class CallableDefaultValueModel(RESTFrameworkModel):
 | 
			
		|||
 | 
			
		||||
class ManyToManyModel(RESTFrameworkModel):
 | 
			
		||||
    rel = models.ManyToManyField(Anchor)
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
class ReadOnlyManyToManyModel(RESTFrameworkModel):
 | 
			
		||||
    text = models.CharField(max_length=100, default='anchor')
 | 
			
		||||
    rel = models.ManyToManyField(Anchor)
 | 
			
		||||
       
 | 
			
		||||
# Models to test generic relations
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -246,6 +246,60 @@ class ManyToManyTests(TestCase):
 | 
			
		|||
        self.assertEquals(len(ManyToManyModel.objects.all()), 2)
 | 
			
		||||
        self.assertEquals(instance.pk, 2)
 | 
			
		||||
        self.assertEquals(list(instance.rel.all()), [])
 | 
			
		||||
        
 | 
			
		||||
class ReadOnlyManyToManyTests(TestCase):
 | 
			
		||||
    def setUp(self):
 | 
			
		||||
        class ReadOnlyManyToManySerializer(serializers.ModelSerializer):
 | 
			
		||||
            rel =  serializers.ManyRelatedField(readonly=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 readonly=True
 | 
			
		||||
        """
 | 
			
		||||
        new_anchor = Anchor()
 | 
			
		||||
        new_anchor.save()
 | 
			
		||||
        data = {'rel': [self.anchor.id, new_anchor.id]}
 | 
			
		||||
        serializer = self.serializer_class(data, instance=self.instance)
 | 
			
		||||
        self.assertEquals(serializer.is_valid(), True)
 | 
			
		||||
        instance = serializer.save()
 | 
			
		||||
        self.assertEquals(len(ReadOnlyManyToManyModel.objects.all()), 1)
 | 
			
		||||
        self.assertEquals(instance.pk, 1)
 | 
			
		||||
        # rel is still as original (1 entry)
 | 
			
		||||
        self.assertEquals(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 readonly=True
 | 
			
		||||
        """
 | 
			
		||||
        new_anchor = Anchor()
 | 
			
		||||
        new_anchor.save()
 | 
			
		||||
        data = {}
 | 
			
		||||
        serializer = self.serializer_class(data, instance=self.instance)
 | 
			
		||||
        self.assertEquals(serializer.is_valid(), True)
 | 
			
		||||
        instance = serializer.save()
 | 
			
		||||
        self.assertEquals(len(ReadOnlyManyToManyModel.objects.all()), 1)
 | 
			
		||||
        self.assertEquals(instance.pk, 1)
 | 
			
		||||
        # rel is still as original (1 entry)
 | 
			
		||||
        self.assertEquals(list(instance.rel.all()), [self.anchor])
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
class DefaultValueTests(TestCase):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user