Fix failing 'default' on ModelSerializer

This commit is contained in:
Tom Christie 2012-10-26 13:20:30 +01:00
parent fc4614a89c
commit 67f1265e49
3 changed files with 14 additions and 12 deletions

View File

@ -410,7 +410,7 @@ class ModelSerializer(Serializer):
kwargs = {} kwargs = {}
if model_field.has_default(): if model_field.has_default():
kwargs['required'] = False kwargs['required'] = False
kwargs['default'] = model_field.default kwargs['default'] = model_field.get_default()
if model_field.__class__ == models.TextField: if model_field.__class__ == models.TextField:
kwargs['widget'] = widgets.Textarea kwargs['widget'] = widgets.Textarea

View File

@ -62,12 +62,12 @@ class CallableDefaultValueModel(RESTFrameworkModel):
class ManyToManyModel(RESTFrameworkModel): class ManyToManyModel(RESTFrameworkModel):
rel = models.ManyToManyField(Anchor) rel = models.ManyToManyField(Anchor)
class ReadOnlyManyToManyModel(RESTFrameworkModel): class ReadOnlyManyToManyModel(RESTFrameworkModel):
text = models.CharField(max_length=100, default='anchor') text = models.CharField(max_length=100, default='anchor')
rel = models.ManyToManyField(Anchor) rel = models.ManyToManyField(Anchor)
# Models to test generic relations # Models to test generic relations

View File

@ -7,7 +7,7 @@ from rest_framework.tests.models import *
class SubComment(object): class SubComment(object):
def __init__(self, sub_comment): def __init__(self, sub_comment):
self.sub_comment = sub_comment self.sub_comment = sub_comment
class Comment(object): class Comment(object):
def __init__(self, email, content, created): def __init__(self, email, content, created):
@ -18,7 +18,7 @@ class Comment(object):
def __eq__(self, other): def __eq__(self, other):
return all([getattr(self, attr) == getattr(other, attr) return all([getattr(self, attr) == getattr(other, attr)
for attr in ('email', 'content', 'created')]) for attr in ('email', 'content', 'created')])
def get_sub_comment(self): def get_sub_comment(self):
sub_comment = SubComment('And Merry Christmas!') sub_comment = SubComment('And Merry Christmas!')
return sub_comment return sub_comment
@ -29,7 +29,7 @@ class CommentSerializer(serializers.Serializer):
content = serializers.CharField(max_length=1000) content = serializers.CharField(max_length=1000)
created = serializers.DateTimeField() created = serializers.DateTimeField()
sub_comment = serializers.Field(source='get_sub_comment.sub_comment') sub_comment = serializers.Field(source='get_sub_comment.sub_comment')
def restore_object(self, data, instance=None): def restore_object(self, data, instance=None):
if instance is None: if instance is None:
return Comment(**data) return Comment(**data)
@ -42,6 +42,7 @@ class ActionItemSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = ActionItem model = ActionItem
class BasicTests(TestCase): class BasicTests(TestCase):
def setUp(self): def setUp(self):
self.comment = Comment( self.comment = Comment(
@ -73,7 +74,7 @@ class BasicTests(TestCase):
self.assertEquals(serializer.data, expected) self.assertEquals(serializer.data, expected)
def test_retrieve(self): def test_retrieve(self):
serializer = CommentSerializer(instance=self.comment) serializer = CommentSerializer(instance=self.comment)
self.assertEquals(serializer.data, self.expected) self.assertEquals(serializer.data, self.expected)
def test_create(self): def test_create(self):
@ -104,7 +105,7 @@ class ValidationTests(TestCase):
'email': 'tom@example.com', 'email': 'tom@example.com',
'content': 'x' * 1001, 'content': 'x' * 1001,
'created': datetime.datetime(2012, 1, 1) 'created': datetime.datetime(2012, 1, 1)
} }
self.actionitem = ActionItem('Some to do item', self.actionitem = ActionItem('Some to do item',
) )
@ -131,7 +132,7 @@ class ValidationTests(TestCase):
"""Make sure that a boolean value with a 'False' value is not """Make sure that a boolean value with a 'False' value is not
mistaken for not having a default.""" mistaken for not having a default."""
data = { data = {
'title':'Some action item', 'title': 'Some action item',
#No 'done' value. #No 'done' value.
} }
serializer = ActionItemSerializer(data, instance=self.actionitem) serializer = ActionItemSerializer(data, instance=self.actionitem)
@ -295,11 +296,13 @@ class ManyToManyTests(TestCase):
self.assertEquals(len(ManyToManyModel.objects.all()), 2) self.assertEquals(len(ManyToManyModel.objects.all()), 2)
self.assertEquals(instance.pk, 2) self.assertEquals(instance.pk, 2)
self.assertEquals(list(instance.rel.all()), []) self.assertEquals(list(instance.rel.all()), [])
class ReadOnlyManyToManyTests(TestCase): class ReadOnlyManyToManyTests(TestCase):
def setUp(self): def setUp(self):
class ReadOnlyManyToManySerializer(serializers.ModelSerializer): class ReadOnlyManyToManySerializer(serializers.ModelSerializer):
rel = serializers.ManyRelatedField(readonly=True) rel = serializers.ManyRelatedField(readonly=True)
class Meta: class Meta:
model = ReadOnlyManyToManyModel model = ReadOnlyManyToManyModel
@ -317,7 +320,6 @@ class ReadOnlyManyToManyTests(TestCase):
# A serialized representation of the model instance # A serialized representation of the model instance
self.data = {'rel': [self.anchor.id], 'id': 1, 'text': 'anchor'} self.data = {'rel': [self.anchor.id], 'id': 1, 'text': 'anchor'}
def test_update(self): def test_update(self):
""" """
Attempt to update an instance of a model with a ManyToMany Attempt to update an instance of a model with a ManyToMany