mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-01-23 15:54:16 +03:00
Added UUIDField.
This commit is contained in:
parent
4201c9fb01
commit
e8db1834d3
|
@ -23,6 +23,7 @@ import datetime
|
||||||
import decimal
|
import decimal
|
||||||
import inspect
|
import inspect
|
||||||
import re
|
import re
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
class empty:
|
class empty:
|
||||||
|
@ -632,6 +633,23 @@ class URLField(CharField):
|
||||||
self.validators.append(validator)
|
self.validators.append(validator)
|
||||||
|
|
||||||
|
|
||||||
|
class UUIDField(Field):
|
||||||
|
default_error_messages = {
|
||||||
|
'invalid': _('"{value}" is not a valid UUID.'),
|
||||||
|
}
|
||||||
|
|
||||||
|
def to_internal_value(self, data):
|
||||||
|
if not isinstance(data, uuid.UUID):
|
||||||
|
try:
|
||||||
|
return uuid.UUID(data)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
self.fail('invalid', value=data)
|
||||||
|
return data
|
||||||
|
|
||||||
|
def to_representation(self, value):
|
||||||
|
return str(value)
|
||||||
|
|
||||||
|
|
||||||
# Number types...
|
# Number types...
|
||||||
|
|
||||||
class IntegerField(Field):
|
class IntegerField(Field):
|
||||||
|
|
|
@ -702,6 +702,7 @@ class ModelSerializer(Serializer):
|
||||||
you need you should either declare the extra/differing fields explicitly on
|
you need you should either declare the extra/differing fields explicitly on
|
||||||
the serializer class, or simply use a `Serializer` class.
|
the serializer class, or simply use a `Serializer` class.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
_field_mapping = ClassLookupDict({
|
_field_mapping = ClassLookupDict({
|
||||||
models.AutoField: IntegerField,
|
models.AutoField: IntegerField,
|
||||||
models.BigIntegerField: IntegerField,
|
models.BigIntegerField: IntegerField,
|
||||||
|
@ -724,7 +725,8 @@ class ModelSerializer(Serializer):
|
||||||
models.SmallIntegerField: IntegerField,
|
models.SmallIntegerField: IntegerField,
|
||||||
models.TextField: CharField,
|
models.TextField: CharField,
|
||||||
models.TimeField: TimeField,
|
models.TimeField: TimeField,
|
||||||
models.URLField: URLField,
|
models.URLField: URLField
|
||||||
|
# Note: Some version-specific mappings also defined below.
|
||||||
})
|
})
|
||||||
_related_class = PrimaryKeyRelatedField
|
_related_class = PrimaryKeyRelatedField
|
||||||
|
|
||||||
|
@ -1132,6 +1134,10 @@ class ModelSerializer(Serializer):
|
||||||
return NestedSerializer
|
return NestedSerializer
|
||||||
|
|
||||||
|
|
||||||
|
if hasattr(models, 'UUIDField'):
|
||||||
|
ModelSerializer._field_mapping[models.UUIDField] = UUIDField
|
||||||
|
|
||||||
|
|
||||||
class HyperlinkedModelSerializer(ModelSerializer):
|
class HyperlinkedModelSerializer(ModelSerializer):
|
||||||
"""
|
"""
|
||||||
A type of `ModelSerializer` that uses hyperlinked relationships instead
|
A type of `ModelSerializer` that uses hyperlinked relationships instead
|
||||||
|
|
|
@ -4,6 +4,7 @@ from rest_framework import serializers
|
||||||
import datetime
|
import datetime
|
||||||
import django
|
import django
|
||||||
import pytest
|
import pytest
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
|
||||||
# Tests for field keyword arguments and core functionality.
|
# Tests for field keyword arguments and core functionality.
|
||||||
|
@ -467,6 +468,23 @@ class TestURLField(FieldValues):
|
||||||
field = serializers.URLField()
|
field = serializers.URLField()
|
||||||
|
|
||||||
|
|
||||||
|
class TestUUIDField(FieldValues):
|
||||||
|
"""
|
||||||
|
Valid and invalid values for `UUIDField`.
|
||||||
|
"""
|
||||||
|
valid_inputs = {
|
||||||
|
'825d7aeb-05a9-45b5-a5b7-05df87923cda': uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'),
|
||||||
|
'825d7aeb05a945b5a5b705df87923cda': uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda')
|
||||||
|
}
|
||||||
|
invalid_inputs = {
|
||||||
|
'825d7aeb-05a9-45b5-a5b7': ['"825d7aeb-05a9-45b5-a5b7" is not a valid UUID.']
|
||||||
|
}
|
||||||
|
outputs = {
|
||||||
|
uuid.UUID('825d7aeb-05a9-45b5-a5b7-05df87923cda'): '825d7aeb-05a9-45b5-a5b7-05df87923cda'
|
||||||
|
}
|
||||||
|
field = serializers.UUIDField()
|
||||||
|
|
||||||
|
|
||||||
# Number types...
|
# Number types...
|
||||||
|
|
||||||
class TestIntegerField(FieldValues):
|
class TestIntegerField(FieldValues):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user