This commit is contained in:
Norman Jäckel 2014-09-25 18:04:00 +00:00
commit fa9079a542
2 changed files with 33 additions and 1 deletions

View File

@ -55,6 +55,10 @@ class ManyToManyModel(RESTFrameworkModel):
rel = models.ManyToManyField(Anchor, help_text='Some help text.')
class ManyToManyModelTwo(RESTFrameworkModel):
rel = models.ManyToManyField(Anchor, null=True, blank=True, help_text='Some other help text.')
class ReadOnlyManyToManyModel(RESTFrameworkModel):
text = models.CharField(max_length=100, default='anchor')
rel = models.ManyToManyField(Anchor)

View File

@ -10,7 +10,7 @@ from rest_framework import serializers, fields, relations
from tests.models import (
HasPositiveIntegerAsChoice, Album, ActionItem, Anchor, BasicModel,
BlankFieldModel, BlogPost, BlogPostComment, Book, CallableDefaultValueModel,
DefaultValueModel, ManyToManyModel, Person, ReadOnlyManyToManyModel, Photo,
DefaultValueModel, ManyToManyModel, ManyToManyModelTwo, Person, ReadOnlyManyToManyModel, Photo,
RESTFrameworkModel, ForeignKeySource
)
from tests.models import BasicModelSerializer
@ -757,6 +757,34 @@ class MetadataTests(TestCase):
self.assertTrue(isinstance(serializer.data.fields[field_name], field))
class ManyToManyTwoTests(TestCase):
"""
Tests model with a ManyToMany relationship with field attributes
null==True and blank==True.
"""
def setUp(self):
class ManyToManySerializerTwo(serializers.ModelSerializer):
class Meta:
model = ManyToManyModelTwo
self.serializer_class = ManyToManySerializerTwo
# An anchor instance to use for the relationship
self.anchor = Anchor()
self.anchor.save()
def test_create(self):
"""
Create an instance of a model with a ManyToMany relationship.
"""
data = {'rel': [self.anchor.id]}
serializer = self.serializer_class(data=data)
self.assertEqual(serializer.is_valid(), True)
instance = serializer.save()
self.assertEqual(ManyToManyModelTwo.objects.count(), 1)
self.assertEqual(list(instance.rel.all()), [self.anchor])
class ManyToManyTests(TestCase):
def setUp(self):
class ManyToManySerializer(serializers.ModelSerializer):