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
c35b9eb0
Commit
c35b9eb0
authored
Nov 14, 2012
by
Marko Tibold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Processed review comments.
No type checking in .restore_fields() Added missing BytesIO import.
parent
8cdbc0a3
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
15 additions
and
8 deletions
+15
-8
rest_framework/fields.py
+14
-4
rest_framework/serializers.py
+1
-4
No files found.
rest_framework/fields.py
View file @
c35b9eb0
...
@@ -3,6 +3,8 @@ import datetime
...
@@ -3,6 +3,8 @@ import datetime
import
inspect
import
inspect
import
warnings
import
warnings
from
io
import
BytesIO
from
django.core
import
validators
from
django.core
import
validators
from
django.core.exceptions
import
ObjectDoesNotExist
,
ValidationError
from
django.core.exceptions
import
ObjectDoesNotExist
,
ValidationError
from
django.core.urlresolvers
import
resolve
,
get_script_prefix
from
django.core.urlresolvers
import
resolve
,
get_script_prefix
...
@@ -31,6 +33,7 @@ class Field(object):
...
@@ -31,6 +33,7 @@ class Field(object):
creation_counter
=
0
creation_counter
=
0
empty
=
''
empty
=
''
type_name
=
None
type_name
=
None
_use_files
=
None
def
__init__
(
self
,
source
=
None
):
def
__init__
(
self
,
source
=
None
):
self
.
parent
=
None
self
.
parent
=
None
...
@@ -51,7 +54,7 @@ class Field(object):
...
@@ -51,7 +54,7 @@ class Field(object):
self
.
root
=
parent
.
root
or
parent
self
.
root
=
parent
.
root
or
parent
self
.
context
=
self
.
root
.
context
self
.
context
=
self
.
root
.
context
def
field_from_native
(
self
,
data
,
field_name
,
into
):
def
field_from_native
(
self
,
data
,
fi
les
,
fi
eld_name
,
into
):
"""
"""
Given a dictionary and a field name, updates the dictionary `into`,
Given a dictionary and a field name, updates the dictionary `into`,
with the field and it's deserialized value.
with the field and it's deserialized value.
...
@@ -166,7 +169,7 @@ class WritableField(Field):
...
@@ -166,7 +169,7 @@ class WritableField(Field):
if
errors
:
if
errors
:
raise
ValidationError
(
errors
)
raise
ValidationError
(
errors
)
def
field_from_native
(
self
,
data
,
field_name
,
into
):
def
field_from_native
(
self
,
data
,
fi
les
,
fi
eld_name
,
into
):
"""
"""
Given a dictionary and a field name, updates the dictionary `into`,
Given a dictionary and a field name, updates the dictionary `into`,
with the field and it's deserialized value.
with the field and it's deserialized value.
...
@@ -175,6 +178,9 @@ class WritableField(Field):
...
@@ -175,6 +178,9 @@ class WritableField(Field):
return
return
try
:
try
:
if
self
.
_use_files
:
native
=
files
[
field_name
]
else
:
native
=
data
[
field_name
]
native
=
data
[
field_name
]
except
KeyError
:
except
KeyError
:
if
self
.
default
is
not
None
:
if
self
.
default
is
not
None
:
...
@@ -323,7 +329,7 @@ class RelatedField(WritableField):
...
@@ -323,7 +329,7 @@ class RelatedField(WritableField):
value
=
getattr
(
obj
,
self
.
source
or
field_name
)
value
=
getattr
(
obj
,
self
.
source
or
field_name
)
return
self
.
to_native
(
value
)
return
self
.
to_native
(
value
)
def
field_from_native
(
self
,
data
,
field_name
,
into
):
def
field_from_native
(
self
,
data
,
fi
les
,
fi
eld_name
,
into
):
if
self
.
read_only
:
if
self
.
read_only
:
return
return
...
@@ -341,7 +347,7 @@ class ManyRelatedMixin(object):
...
@@ -341,7 +347,7 @@ class ManyRelatedMixin(object):
value
=
getattr
(
obj
,
self
.
source
or
field_name
)
value
=
getattr
(
obj
,
self
.
source
or
field_name
)
return
[
self
.
to_native
(
item
)
for
item
in
value
.
all
()]
return
[
self
.
to_native
(
item
)
for
item
in
value
.
all
()]
def
field_from_native
(
self
,
data
,
field_name
,
into
):
def
field_from_native
(
self
,
data
,
fi
les
,
fi
eld_name
,
into
):
if
self
.
read_only
:
if
self
.
read_only
:
return
return
...
@@ -907,8 +913,10 @@ class FloatField(WritableField):
...
@@ -907,8 +913,10 @@ class FloatField(WritableField):
class
FileField
(
WritableField
):
class
FileField
(
WritableField
):
_use_files
=
True
type_name
=
'FileField'
type_name
=
'FileField'
widget
=
widgets
.
FileInput
widget
=
widgets
.
FileInput
default_error_messages
=
{
default_error_messages
=
{
'invalid'
:
_
(
"No file was submitted. Check the encoding type on the form."
),
'invalid'
:
_
(
"No file was submitted. Check the encoding type on the form."
),
'missing'
:
_
(
"No file was submitted."
),
'missing'
:
_
(
"No file was submitted."
),
...
@@ -951,6 +959,8 @@ class FileField(WritableField):
...
@@ -951,6 +959,8 @@ class FileField(WritableField):
class
ImageField
(
FileField
):
class
ImageField
(
FileField
):
_use_files
=
True
default_error_messages
=
{
default_error_messages
=
{
'invalid_image'
:
_
(
"Upload a valid image. The file you uploaded was either not an image or a corrupted image."
),
'invalid_image'
:
_
(
"Upload a valid image. The file you uploaded was either not an image or a corrupted image."
),
}
}
...
...
rest_framework/serializers.py
View file @
c35b9eb0
...
@@ -198,10 +198,7 @@ class BaseSerializer(Field):
...
@@ -198,10 +198,7 @@ class BaseSerializer(Field):
reverted_data
=
{}
reverted_data
=
{}
for
field_name
,
field
in
fields
.
items
():
for
field_name
,
field
in
fields
.
items
():
try
:
try
:
if
isinstance
(
field
,
(
FileField
,
ImageField
)):
field
.
field_from_native
(
data
,
files
,
field_name
,
reverted_data
)
field
.
field_from_native
(
files
,
field_name
,
reverted_data
)
else
:
field
.
field_from_native
(
data
,
field_name
,
reverted_data
)
except
ValidationError
as
err
:
except
ValidationError
as
err
:
self
.
_errors
[
field_name
]
=
list
(
err
.
messages
)
self
.
_errors
[
field_name
]
=
list
(
err
.
messages
)
...
...
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