Skip to content

Added UUIDField. #2448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 23, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion docs/api-guide/fields.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ Corresponds to `django.db.models.fields.URLField`. Uses Django's `django.core.v

**Signature:** `URLField(max_length=200, min_length=None, allow_blank=False)`

## UUIDField

A field that ensures the input is a valid UUID string. The `to_internal_value` method will return a `uuid.UUID` instance. On output the field will return a string in the canonical hyphenated format, for example:

"de305d54-75b4-431b-adb2-eb6b9e546013"

---

# Numeric fields
Expand Down Expand Up @@ -320,7 +326,7 @@ Both the `allow_blank` and `allow_null` are valid options on `ChoiceField`, alth

## MultipleChoiceField

A field that can accept a set of zero, one or many values, chosen from a limited set of choices. Takes a single mandatory argument. `to_internal_representation` returns a `set` containing the selected values.
A field that can accept a set of zero, one or many values, chosen from a limited set of choices. Takes a single mandatory argument. `to_internal_value` returns a `set` containing the selected values.

**Signature:** `MultipleChoiceField(choices)`

Expand Down
18 changes: 18 additions & 0 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import decimal
import inspect
import re
import uuid


class empty:
Expand Down Expand Up @@ -632,6 +633,23 @@ def __init__(self, **kwargs):
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...

class IntegerField(Field):
Expand Down
8 changes: 7 additions & 1 deletion rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,7 @@ class ModelSerializer(Serializer):
you need you should either declare the extra/differing fields explicitly on
the serializer class, or simply use a `Serializer` class.
"""

_field_mapping = ClassLookupDict({
models.AutoField: IntegerField,
models.BigIntegerField: IntegerField,
Expand All @@ -724,7 +725,8 @@ class ModelSerializer(Serializer):
models.SmallIntegerField: IntegerField,
models.TextField: CharField,
models.TimeField: TimeField,
models.URLField: URLField,
models.URLField: URLField
# Note: Some version-specific mappings also defined below.
})
_related_class = PrimaryKeyRelatedField

Expand Down Expand Up @@ -1132,6 +1134,10 @@ class Meta:
return NestedSerializer


if hasattr(models, 'UUIDField'):
ModelSerializer._field_mapping[models.UUIDField] = UUIDField


class HyperlinkedModelSerializer(ModelSerializer):
"""
A type of `ModelSerializer` that uses hyperlinked relationships instead
Expand Down
3 changes: 3 additions & 0 deletions rest_framework/utils/field_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def __getitem__(self, key):
return self.mapping[cls]
raise KeyError('Class %s not found in lookup.', cls.__name__)

def __setitem__(self, key, value):
self.mapping[key] = value


def needs_label(model_field, field_name):
"""
Expand Down
18 changes: 18 additions & 0 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import datetime
import django
import pytest
import uuid


# Tests for field keyword arguments and core functionality.
Expand Down Expand Up @@ -467,6 +468,23 @@ class TestURLField(FieldValues):
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...

class TestIntegerField(FieldValues):
Expand Down