Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
django-rest-framework
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
django-rest-framework
Commits
0c66c7cf
Commit
0c66c7cf
authored
Jun 03, 2015
by
Xavier Ordoquy
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3000 from linovia/ovangle/uuid_format2
Add 'format' argument to UUIDField
parents
bb974856
ea00fc7a
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
42 additions
and
3 deletions
+42
-3
docs/api-guide/fields.md
+9
-0
rest_framework/fields.py
+19
-2
tests/test_fields.py
+14
-1
No files found.
docs/api-guide/fields.md
View file @
0c66c7cf
...
...
@@ -183,6 +183,15 @@ A field that ensures the input is a valid UUID string. The `to_internal_value` m
"de305d54-75b4-431b-adb2-eb6b9e546013"
**Signature:**
`UUIDField(format='hex_verbose')`
-
`format`
: Determines the representation format of the uuid value
-
`'hex_verbose'`
- The cannoncical hex representation, including hyphens:
`"5ce0e9a5-5ffa-654b-cee0-1238041fb31a"`
-
`'hex'`
- The compact hex representation of the UUID, not including hyphens:
`"5ce0e9a55ffa654bcee01238041fb31a"`
-
`'int'`
- A 128 bit integer representation of the UUID:
`"123456789012312313134124512351145145114"`
-
`'urn'`
- RFC 4122 URN representation of the UUID:
`"urn:uuid:5ce0e9a5-5ffa-654b-cee0-1238041fb31a"`
Changing the
`format`
parameters only affects representation values. All formats are accepted by
`to_internal_value`
---
# Numeric fields
...
...
rest_framework/fields.py
View file @
0c66c7cf
...
...
@@ -639,20 +639,37 @@ class URLField(CharField):
class
UUIDField
(
Field
):
valid_formats
=
(
'hex_verbose'
,
'hex'
,
'int'
,
'urn'
)
default_error_messages
=
{
'invalid'
:
_
(
'"{value}" is not a valid UUID.'
),
}
def
__init__
(
self
,
**
kwargs
):
self
.
uuid_format
=
kwargs
.
pop
(
'format'
,
'hex_verbose'
)
if
self
.
uuid_format
not
in
self
.
valid_formats
:
raise
ValueError
(
'Invalid format for uuid representation. '
'Must be one of "{0}"'
.
format
(
'", "'
.
join
(
self
.
valid_formats
))
)
super
(
UUIDField
,
self
)
.
__init__
(
**
kwargs
)
def
to_internal_value
(
self
,
data
):
if
not
isinstance
(
data
,
uuid
.
UUID
):
try
:
return
uuid
.
UUID
(
data
)
if
isinstance
(
data
,
six
.
integer_types
):
return
uuid
.
UUID
(
int
=
data
)
else
:
return
uuid
.
UUID
(
hex
=
data
)
except
(
ValueError
,
TypeError
):
self
.
fail
(
'invalid'
,
value
=
data
)
return
data
def
to_representation
(
self
,
value
):
return
str
(
value
)
if
self
.
uuid_format
==
'hex_verbose'
:
return
str
(
value
)
else
:
return
getattr
(
value
,
self
.
uuid_format
)
# Number types...
...
...
tests/test_fields.py
View file @
0c66c7cf
...
...
@@ -525,7 +525,9 @@ class TestUUIDField(FieldValues):
"""
valid_inputs
=
{
'825d7aeb-05a9-45b5-a5b7-05df87923cda'
:
uuid
.
UUID
(
'825d7aeb-05a9-45b5-a5b7-05df87923cda'
),
'825d7aeb05a945b5a5b705df87923cda'
:
uuid
.
UUID
(
'825d7aeb-05a9-45b5-a5b7-05df87923cda'
)
'825d7aeb05a945b5a5b705df87923cda'
:
uuid
.
UUID
(
'825d7aeb-05a9-45b5-a5b7-05df87923cda'
),
'urn:uuid:213b7d9b-244f-410d-828c-dabce7a2615d'
:
uuid
.
UUID
(
'213b7d9b-244f-410d-828c-dabce7a2615d'
),
284758210125106368185219588917561929842
:
uuid
.
UUID
(
'd63a6fb6-88d5-40c7-a91c-9edf73283072'
)
}
invalid_inputs
=
{
'825d7aeb-05a9-45b5-a5b7'
:
[
'"825d7aeb-05a9-45b5-a5b7" is not a valid UUID.'
]
...
...
@@ -535,6 +537,17 @@ class TestUUIDField(FieldValues):
}
field
=
serializers
.
UUIDField
()
def
_test_format
(
self
,
uuid_format
,
formatted_uuid_0
):
field
=
serializers
.
UUIDField
(
format
=
uuid_format
)
assert
field
.
to_representation
(
uuid
.
UUID
(
int
=
0
))
==
formatted_uuid_0
assert
field
.
to_internal_value
(
formatted_uuid_0
)
==
uuid
.
UUID
(
int
=
0
)
def
test_formats
(
self
):
self
.
_test_format
(
'int'
,
0
)
self
.
_test_format
(
'hex_verbose'
,
'00000000-0000-0000-0000-000000000000'
)
self
.
_test_format
(
'urn'
,
'urn:uuid:00000000-0000-0000-0000-000000000000'
)
self
.
_test_format
(
'hex'
,
'0'
*
32
)
# Number types...
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment