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
45dc44b2
Commit
45dc44b2
authored
Dec 04, 2014
by
Tymur Maryokhin
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of github.com:tomchristie/django-rest-framework
parents
001884a1
e8cbf41b
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
26 additions
and
22 deletions
+26
-22
docs/tutorial/1-serialization.md
+8
-8
rest_framework/renderers.py
+4
-0
rest_framework/serializers.py
+14
-14
No files found.
docs/tutorial/1-serialization.md
View file @
45dc44b2
...
...
@@ -110,21 +110,21 @@ The first thing we need to get started on our Web API is to provide a way of ser
style = serializers.ChoiceField(choices=STYLE_CHOICES,
default='friendly')
def create(self, validated_
attrs
):
def create(self, validated_
data
):
"""
Create and return a new `Snippet` instance, given the validated data.
"""
return Snippet.objects.create(**validated_
attrs
)
return Snippet.objects.create(**validated_
data
)
def update(self, instance, validated_
attrs
):
def update(self, instance, validated_
data
):
"""
Update and return an existing `Snippet` instance, given the validated data.
"""
instance.title = validated_
attrs
.get('title', instance.title)
instance.code = validated_
attrs
.get('code', instance.code)
instance.linenos = validated_
attrs
.get('linenos', instance.linenos)
instance.language = validated_
attrs
.get('language', instance.language)
instance.style = validated_
attrs
.get('style', instance.style)
instance.title = validated_
data
.get('title', instance.title)
instance.code = validated_
data
.get('code', instance.code)
instance.linenos = validated_
data
.get('linenos', instance.linenos)
instance.language = validated_
data
.get('language', instance.language)
instance.style = validated_
data
.get('style', instance.style)
instance.save()
return instance
...
...
rest_framework/renderers.py
View file @
45dc44b2
...
...
@@ -374,6 +374,10 @@ class HTMLFormRenderer(BaseRenderer):
'base_template'
:
'input.html'
,
'input_type'
:
'time'
},
serializers
.
FileField
:
{
'base_template'
:
'input.html'
,
'input_type'
:
'file'
},
serializers
.
BooleanField
:
{
'base_template'
:
'checkbox.html'
},
...
...
rest_framework/serializers.py
View file @
45dc44b2
...
...
@@ -608,20 +608,20 @@ class ModelSerializer(Serializer):
})
_related_class
=
PrimaryKeyRelatedField
def
create
(
self
,
validated_
attrs
):
def
create
(
self
,
validated_
data
):
"""
We have a bit of extra checking around this in order to provide
descriptive messages when something goes wrong, but this method is
essentially just:
return ExampleModel.objects.create(**validated_
attrs
)
return ExampleModel.objects.create(**validated_
data
)
If there are many to many fields present on the instance then they
cannot be set until the model is instantiated, in which case the
implementation is like so:
example_relationship = validated_
attrs
.pop('example_relationship')
instance = ExampleModel.objects.create(**validated_
attrs
)
example_relationship = validated_
data
.pop('example_relationship')
instance = ExampleModel.objects.create(**validated_
data
)
instance.example_relationship = example_relationship
return instance
...
...
@@ -633,8 +633,8 @@ class ModelSerializer(Serializer):
# If we don't do this explicitly they'd likely get a confusing
# error at the point of calling `Model.objects.create()`.
assert
not
any
(
isinstance
(
field
,
BaseSerializer
)
and
not
field
.
read_only
for
field
in
self
.
fields
.
value
s
()
isinstance
(
field
,
BaseSerializer
)
and
(
key
in
validated_attrs
)
for
key
,
field
in
self
.
fields
.
item
s
()
),
(
'The `.create()` method does not suport nested writable fields '
'by default. Write an explicit `.create()` method for serializer '
...
...
@@ -644,17 +644,17 @@ class ModelSerializer(Serializer):
ModelClass
=
self
.
Meta
.
model
# Remove many-to-many relationships from validated_
attrs
.
# Remove many-to-many relationships from validated_
data
.
# They are not valid arguments to the default `.create()` method,
# as they require that the instance has already been saved.
info
=
model_meta
.
get_field_info
(
ModelClass
)
many_to_many
=
{}
for
field_name
,
relation_info
in
info
.
relations
.
items
():
if
relation_info
.
to_many
and
(
field_name
in
validated_
attrs
):
many_to_many
[
field_name
]
=
validated_
attrs
.
pop
(
field_name
)
if
relation_info
.
to_many
and
(
field_name
in
validated_
data
):
many_to_many
[
field_name
]
=
validated_
data
.
pop
(
field_name
)
try
:
instance
=
ModelClass
.
objects
.
create
(
**
validated_
attrs
)
instance
=
ModelClass
.
objects
.
create
(
**
validated_
data
)
except
TypeError
as
exc
:
msg
=
(
'Got a `TypeError` when calling `
%
s.objects.create()`. '
...
...
@@ -679,10 +679,10 @@ class ModelSerializer(Serializer):
return
instance
def
update
(
self
,
instance
,
validated_
attrs
):
def
update
(
self
,
instance
,
validated_
data
):
assert
not
any
(
isinstance
(
field
,
BaseSerializer
)
and
not
field
.
read_only
for
field
in
self
.
fields
.
value
s
()
isinstance
(
field
,
BaseSerializer
)
and
(
key
in
validated_attrs
)
for
key
,
field
in
self
.
fields
.
item
s
()
),
(
'The `.update()` method does not suport nested writable fields '
'by default. Write an explicit `.update()` method for serializer '
...
...
@@ -690,7 +690,7 @@ class ModelSerializer(Serializer):
(
self
.
__class__
.
__module__
,
self
.
__class__
.
__name__
)
)
for
attr
,
value
in
validated_
attrs
.
items
():
for
attr
,
value
in
validated_
data
.
items
():
setattr
(
instance
,
attr
,
value
)
instance
.
save
()
return
instance
...
...
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