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
ad060aa3
Commit
ad060aa3
authored
Nov 15, 2014
by
Gregor Müllegger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More helpful error message when default `.create` fails. Closes #2013.
parent
4e035184
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
1 deletions
+41
-1
rest_framework/serializers.py
+13
-1
tests/test_model_serializer.py
+28
-0
No files found.
rest_framework/serializers.py
View file @
ad060aa3
...
...
@@ -34,6 +34,7 @@ from rest_framework.validators import (
)
import
copy
import
inspect
import
sys
import
warnings
# Note: We do the following so that users of the framework can use this style:
...
...
@@ -593,7 +594,18 @@ class ModelSerializer(Serializer):
if
relation_info
.
to_many
and
(
field_name
in
validated_attrs
):
many_to_many
[
field_name
]
=
validated_attrs
.
pop
(
field_name
)
instance
=
ModelClass
.
objects
.
create
(
**
validated_attrs
)
try
:
instance
=
ModelClass
.
objects
.
create
(
**
validated_attrs
)
except
TypeError
as
exc
:
msg
=
(
'The mentioned argument might be a field on the serializer '
'that is not part of the model. You need to override the '
'create() method in your ModelSerializer subclass to support '
'this.'
)
six
.
reraise
(
type
(
exc
),
type
(
exc
)(
str
(
exc
)
+
'. '
+
msg
),
sys
.
exc_info
()[
2
])
# Save many-to-many relationships after the instance is created.
if
many_to_many
:
...
...
tests/test_model_serializer.py
View file @
ad060aa3
...
...
@@ -10,6 +10,7 @@ from django.core.validators import MaxValueValidator, MinValueValidator, MinLeng
from
django.db
import
models
from
django.test
import
TestCase
from
rest_framework
import
serializers
import
pytest
def
dedent
(
blocktext
):
...
...
@@ -26,6 +27,10 @@ class CustomField(models.Field):
pass
class
OneFieldModel
(
models
.
Model
):
char_field
=
models
.
CharField
(
max_length
=
100
)
class
RegularFieldsModel
(
models
.
Model
):
"""
A model class for testing regular flat fields.
...
...
@@ -68,6 +73,29 @@ class FieldOptionsModel(models.Model):
choices_field
=
models
.
CharField
(
max_length
=
100
,
choices
=
COLOR_CHOICES
)
class
TestModelSerializer
(
TestCase
):
def
test_create_method
(
self
):
class
TestSerializer
(
serializers
.
ModelSerializer
):
non_model_field
=
serializers
.
CharField
()
class
Meta
:
model
=
OneFieldModel
fields
=
(
'char_field'
,
'non_model_field'
)
serializer
=
TestSerializer
(
data
=
{
'char_field'
:
'foo'
,
'non_model_field'
:
'bar'
,
})
serializer
.
is_valid
()
with
pytest
.
raises
(
TypeError
):
serializer
.
save
()
try
:
serializer
.
save
()
except
TypeError
as
exc
:
assert
'ModelSerializer'
in
str
(
exc
)
class
TestRegularFieldMappings
(
TestCase
):
def
test_regular_fields
(
self
):
"""
...
...
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