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
270c7acd
Commit
270c7acd
authored
Dec 01, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor validtors tweak
parent
22c5b863
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
30 additions
and
4 deletions
+30
-4
docs/topics/3.0-announcement.md
+2
-1
rest_framework/serializers.py
+28
-3
No files found.
docs/topics/3.0-announcement.md
View file @
270c7acd
...
@@ -727,7 +727,8 @@ The `UniqueTogetherValidator` should be applied to a serializer, and takes a `qu
...
@@ -727,7 +727,8 @@ The `UniqueTogetherValidator` should be applied to a serializer, and takes a `qu
position = serializers.IntegerField()
position = serializers.IntegerField()
name = serializers.CharField(max_length=100)
name = serializers.CharField(max_length=100)
default_validators = [UniqueTogetherValidator(
class Meta
:
validators = [UniqueTogetherValidator(
queryset=RaceResult.objects.all(),
queryset=RaceResult.objects.all(),
fields=('category', 'position')
fields=('category', 'position')
)]
)]
...
...
rest_framework/serializers.py
View file @
270c7acd
...
@@ -601,6 +601,26 @@ class ModelSerializer(Serializer):
...
@@ -601,6 +601,26 @@ class ModelSerializer(Serializer):
_related_class
=
PrimaryKeyRelatedField
_related_class
=
PrimaryKeyRelatedField
def
create
(
self
,
validated_attrs
):
def
create
(
self
,
validated_attrs
):
"""
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)
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)
instance.example_relationship = example_relationship
return instance
The default implementation also does not handle nested relationships.
If you want to support writable nested relationships you'll need
to write an explicit `.create()` method.
"""
# Check that the user isn't trying to handle a writable nested field.
# Check that the user isn't trying to handle a writable nested field.
# If we don't do this explicitly they'd likely get a confusing
# If we don't do this explicitly they'd likely get a confusing
# error at the point of calling `Model.objects.create()`.
# error at the point of calling `Model.objects.create()`.
...
@@ -651,14 +671,19 @@ class ModelSerializer(Serializer):
...
@@ -651,14 +671,19 @@ class ModelSerializer(Serializer):
return
instance
return
instance
def
get_validators
(
self
):
def
get_validators
(
self
):
# If the validators have been declared explicitly then use that.
validators
=
getattr
(
getattr
(
self
,
'Meta'
,
None
),
'validators'
,
None
)
if
validators
is
not
None
:
return
validators
# Determine the default set of validators.
validators
=
[]
model_class
=
self
.
Meta
.
model
field_names
=
set
([
field_names
=
set
([
field
.
source
for
field
in
self
.
fields
.
values
()
field
.
source
for
field
in
self
.
fields
.
values
()
if
(
field
.
source
!=
'*'
)
and
(
'.'
not
in
field
.
source
)
if
(
field
.
source
!=
'*'
)
and
(
'.'
not
in
field
.
source
)
])
])
validators
=
getattr
(
getattr
(
self
,
'Meta'
,
None
),
'validators'
,
[])
model_class
=
self
.
Meta
.
model
# Note that we make sure to check `unique_together` both on the
# Note that we make sure to check `unique_together` both on the
# base model class, but also on any parent classes.
# base model class, but also on any parent classes.
for
parent_class
in
[
model_class
]
+
list
(
model_class
.
_meta
.
parents
.
keys
()):
for
parent_class
in
[
model_class
]
+
list
(
model_class
.
_meta
.
parents
.
keys
()):
...
...
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