diff --git a/docs/api-guide/fields.md b/docs/api-guide/fields.md index df54ea021..dc9ab0452 100644 --- a/docs/api-guide/fields.md +++ b/docs/api-guide/fields.md @@ -73,18 +73,35 @@ These fields represent basic datatypes, and support both reading and writing val ## BooleanField +A Boolean representation, corresponds to `django.db.models.fields.BooleanField`. + ## CharField +A text representation, optionally validates the text to be shorter than `max_length` and longer than `min_length`, corresponds to `django.db.models.fields.CharField` +or `django.db.models.fields.TextField`. + +**Signature:** `CharField([max_length=[, min_length=]])` + ## EmailField +A text representation, validates the text to be a valid e-mail adress. Corresponds to `django.db.models.fields.EmailField` + ## DateField +A date representation. Corresponds to `django.db.models.fields.DateField` + ## DateTimeField +A date and time representation. Corresponds to `django.db.models.fields.DateTimeField` + ## IntegerField +An integer representation. Corresponds to `django.db.models.fields.IntegerField`, `django.db.models.fields.SmallIntegerField`, `django.db.models.fields.PositiveIntegerField` and `django.db.models.fields.PositiveSmallIntegerField` + ## FloatField +A floating point representation. Corresponds to `django.db.models.fields.FloatField`. + --- # Relational Fields diff --git a/rest_framework/fields.py b/rest_framework/fields.py index bb9a523d6..5d13bd55f 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -409,6 +409,13 @@ class BooleanField(WritableField): return False raise ValidationError(self.error_messages['invalid'] % value) +class TextField(WritableField): + type_name = 'TextField' + + def from_native(self, value): + if isinstance(value, basestring) or value is None: + return value + return smart_unicode(value) class CharField(WritableField): type_name = 'CharField' diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 063300172..13f8cde21 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -377,10 +377,14 @@ class ModelSerializer(Serializer): field_mapping = { models.FloatField: FloatField, models.IntegerField: IntegerField, + models.PositiveIntegerField: IntegerField, + models.SmallIntegerField: IntegerField, + models.PositiveSmallIntegerField: IntegerField, models.DateTimeField: DateTimeField, models.DateField: DateField, models.EmailField: EmailField, models.CharField: CharField, + models.TextField: CharField, models.CommaSeparatedIntegerField: CharField, models.BooleanField: BooleanField, } diff --git a/rest_framework/tests/serializer.py b/rest_framework/tests/serializer.py index 256987ad0..7208d6a54 100644 --- a/rest_framework/tests/serializer.py +++ b/rest_framework/tests/serializer.py @@ -28,6 +28,26 @@ class CommentSerializer(serializers.Serializer): return instance +class LongText(object): + def __init__(self, content): + self.content = content + + def __eq__(self, other): + return all([getattr(self, attr) == getattr(other, attr) + for attr in ('content',)]) + + +class LongTextSerializer(serializers.Serializer): + content = serializers.TextField() + + def restore_object(self, data, instance=None): + if instance is None: + return LongText(**data) + for key, val in data.items(): + setattr(instance, key, val) + return instance + + class BasicTests(TestCase): def setUp(self): self.comment = Comment( @@ -82,6 +102,7 @@ class ValidationTests(TestCase): 'content': 'x' * 1001, 'created': datetime.datetime(2012, 1, 1) } + self.long_text = LongText('test test test test') def test_create(self): serializer = CommentSerializer(self.data) @@ -102,6 +123,14 @@ class ValidationTests(TestCase): self.assertEquals(serializer.is_valid(), False) self.assertEquals(serializer.errors, {'email': [u'This field is required.']}) + def test_update_long_text(self): + data = { + 'content' : 'Lorem ipsum dolor sit amet.' + } + serializer = LongTextSerializer(data, self.long_text) + self.assertEquals(serializer.is_valid(), True) + self.assertEquals(data['content'], self.long_text.content) + class MetadataTests(TestCase): def test_empty(self):