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
76ac641f
Commit
76ac641f
authored
Dec 02, 2014
by
Tom Christie
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor tweaks for helpful message on Model.objects.create() failure.
parent
54b7b328
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
17 additions
and
16 deletions
+17
-16
rest_framework/serializers.py
+14
-9
tests/test_model_serializer.py
+3
-7
No files found.
rest_framework/serializers.py
View file @
76ac641f
...
...
@@ -35,7 +35,6 @@ 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:
...
...
@@ -658,14 +657,20 @@ class ModelSerializer(Serializer):
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
])
'Got a `TypeError` when calling `
%
s.objects.create()`. '
'This may be because you have a writable field on the '
'serializer class that is not a valid argument to '
'`
%
s.objects.create()`. You may need to make the field '
'read-only, or override the
%
s.create() method to handle '
'this correctly.
\n
Original exception text was:
%
s.'
%
(
ModelClass
.
__name__
,
ModelClass
.
__name__
,
self
.
__class__
.
__name__
,
exc
)
)
raise
TypeError
(
msg
)
# Save many-to-many relationships after the instance is created.
if
many_to_many
:
...
...
tests/test_model_serializer.py
View file @
76ac641f
...
...
@@ -10,7 +10,6 @@ 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
):
...
...
@@ -87,13 +86,10 @@ class TestModelSerializer(TestCase):
'non_model_field'
:
'bar'
,
})
serializer
.
is_valid
()
with
pytest
.
raises
(
TypeError
)
:
with
self
.
assertRaises
(
TypeError
)
as
excinfo
:
serializer
.
save
()
try
:
serializer
.
save
()
except
TypeError
as
exc
:
assert
'ModelSerializer'
in
str
(
exc
)
msginitial
=
'Got a `TypeError` when calling `OneFieldModel.objects.create()`.'
assert
str
(
excinfo
.
exception
)
.
startswith
(
msginitial
)
class
TestRegularFieldMappings
(
TestCase
):
...
...
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