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
ab25d706
Commit
ab25d706
authored
Dec 03, 2014
by
Martin Tschammer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Renamed validated_attrs to validated_data to be more in line with other similar code.
parent
5f2f54b7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
18 deletions
+18
-18
docs/tutorial/1-serialization.md
+8
-8
rest_framework/serializers.py
+10
-10
No files found.
docs/tutorial/1-serialization.md
View file @
ab25d706
...
@@ -110,21 +110,21 @@ The first thing we need to get started on our Web API is to provide a way of ser
...
@@ -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,
style = serializers.ChoiceField(choices=STYLE_CHOICES,
default='friendly')
default='friendly')
def create(self, validated_
attrs
):
def create(self, validated_
data
):
"""
"""
Create and return a new `Snippet` instance, given the 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.
Update and return an existing `Snippet` instance, given the validated data.
"""
"""
instance.title = validated_
attrs
.get('title', instance.title)
instance.title = validated_
data
.get('title', instance.title)
instance.code = validated_
attrs
.get('code', instance.code)
instance.code = validated_
data
.get('code', instance.code)
instance.linenos = validated_
attrs
.get('linenos', instance.linenos)
instance.linenos = validated_
data
.get('linenos', instance.linenos)
instance.language = validated_
attrs
.get('language', instance.language)
instance.language = validated_
data
.get('language', instance.language)
instance.style = validated_
attrs
.get('style', instance.style)
instance.style = validated_
data
.get('style', instance.style)
instance.save()
instance.save()
return instance
return instance
...
...
rest_framework/serializers.py
View file @
ab25d706
...
@@ -608,20 +608,20 @@ class ModelSerializer(Serializer):
...
@@ -608,20 +608,20 @@ class ModelSerializer(Serializer):
})
})
_related_class
=
PrimaryKeyRelatedField
_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
We have a bit of extra checking around this in order to provide
descriptive messages when something goes wrong, but this method is
descriptive messages when something goes wrong, but this method is
essentially just:
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
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
cannot be set until the model is instantiated, in which case the
implementation is like so:
implementation is like so:
example_relationship = validated_
attrs
.pop('example_relationship')
example_relationship = validated_
data
.pop('example_relationship')
instance = ExampleModel.objects.create(**validated_
attrs
)
instance = ExampleModel.objects.create(**validated_
data
)
instance.example_relationship = example_relationship
instance.example_relationship = example_relationship
return instance
return instance
...
@@ -644,17 +644,17 @@ class ModelSerializer(Serializer):
...
@@ -644,17 +644,17 @@ class ModelSerializer(Serializer):
ModelClass
=
self
.
Meta
.
model
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,
# They are not valid arguments to the default `.create()` method,
# as they require that the instance has already been saved.
# as they require that the instance has already been saved.
info
=
model_meta
.
get_field_info
(
ModelClass
)
info
=
model_meta
.
get_field_info
(
ModelClass
)
many_to_many
=
{}
many_to_many
=
{}
for
field_name
,
relation_info
in
info
.
relations
.
items
():
for
field_name
,
relation_info
in
info
.
relations
.
items
():
if
relation_info
.
to_many
and
(
field_name
in
validated_
attrs
):
if
relation_info
.
to_many
and
(
field_name
in
validated_
data
):
many_to_many
[
field_name
]
=
validated_
attrs
.
pop
(
field_name
)
many_to_many
[
field_name
]
=
validated_
data
.
pop
(
field_name
)
try
:
try
:
instance
=
ModelClass
.
objects
.
create
(
**
validated_
attrs
)
instance
=
ModelClass
.
objects
.
create
(
**
validated_
data
)
except
TypeError
as
exc
:
except
TypeError
as
exc
:
msg
=
(
msg
=
(
'Got a `TypeError` when calling `
%
s.objects.create()`. '
'Got a `TypeError` when calling `
%
s.objects.create()`. '
...
@@ -679,7 +679,7 @@ class ModelSerializer(Serializer):
...
@@ -679,7 +679,7 @@ class ModelSerializer(Serializer):
return
instance
return
instance
def
update
(
self
,
instance
,
validated_
attrs
):
def
update
(
self
,
instance
,
validated_
data
):
assert
not
any
(
assert
not
any
(
isinstance
(
field
,
BaseSerializer
)
and
not
field
.
read_only
isinstance
(
field
,
BaseSerializer
)
and
not
field
.
read_only
for
field
in
self
.
fields
.
values
()
for
field
in
self
.
fields
.
values
()
...
@@ -690,7 +690,7 @@ class ModelSerializer(Serializer):
...
@@ -690,7 +690,7 @@ class ModelSerializer(Serializer):
(
self
.
__class__
.
__module__
,
self
.
__class__
.
__name__
)
(
self
.
__class__
.
__module__
,
self
.
__class__
.
__name__
)
)
)
for
attr
,
value
in
validated_
attrs
.
items
():
for
attr
,
value
in
validated_
data
.
items
():
setattr
(
instance
,
attr
,
value
)
setattr
(
instance
,
attr
,
value
)
instance
.
save
()
instance
.
save
()
return
instance
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