mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Merge pull request #295 from ulmus/restframework2
Added TextField to serializer
This commit is contained in:
commit
e88ca9637b
|
@ -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=<Integer>[, min_length=<Integer>]])`
|
||||
|
||||
## 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
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in New Issue
Block a user