Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
dbb435c
Added missing default style for FileField
kchang Nov 12, 2014
ad060aa
More helpful error message when default `.create` fails. Closes #2013.
gregmuellegger Nov 15, 2014
2f03483
Removed unused models
maryokhin Nov 29, 2014
dd9d40d
Moved non-conflicting models
maryokhin Nov 29, 2014
e2ea98e
Fixed typos
maryokhin Nov 29, 2014
bc0c25d
Consolidate Django and test requirements
jpadilla Dec 1, 2014
0a91999
Merge branch 'master' of github.com:tomchristie/django-rest-framework
maryokhin Dec 2, 2014
d847e33
Merge pull request #2161 from NextHub/master
lovelydinosaur Dec 2, 2014
deec61e
Merge pull request #2173 from jpadilla/master
lovelydinosaur Dec 2, 2014
f4fc467
Promote 'many_init' to public API. Closes #2152.
lovelydinosaur Dec 2, 2014
53f5276
Not allow to pass an empty actions to viewset.as_view(). Refs issue #…
BrickXu Dec 2, 2014
6ac79b8
Document Field.fail(). Closes #2147.
lovelydinosaur Dec 2, 2014
79e18a2
Raise assertion error if calling .save() on a serializer with errors.…
lovelydinosaur Dec 2, 2014
54b7b32
Merge branch 'fixes/2013' of git://github.com/gregmuellegger/django-r…
lovelydinosaur Dec 2, 2014
76ac641
Minor tweaks for helpful message on Model.objects.create() failure.
lovelydinosaur Dec 2, 2014
41045c1
Merge branch 'gregmuellegger-fixes/2013'
lovelydinosaur Dec 2, 2014
e30e3f6
Update README with 3.0 info.
lovelydinosaur Dec 2, 2014
0359e92
FileUploadParser. Raising StopFutureHandlers removes any handlers not…
lovelydinosaur Dec 2, 2014
84cff98
fix typo
daaray Dec 2, 2014
e335ad4
Merge pull request #2181 from daaray/patch-2
lovelydinosaur Dec 2, 2014
33096a1
BindingDict inherits from collections.MutableMapping. Closes #2135.
lovelydinosaur Dec 2, 2014
2123685
Merge branch 'master' of https://github.com/tomchristie/django-rest-f…
lovelydinosaur Dec 2, 2014
5ad22ae
Updated serializers documentation
cript0nauta Dec 2, 2014
95d57e6
Merge pull request #2182 from sh4r3m4n/master
lovelydinosaur Dec 2, 2014
9397a65
Use svg version of travis build status badge
eiriksm Dec 3, 2014
71a8cb2
Merge pull request #2185 from eiriksm/patch-1
lovelydinosaur Dec 3, 2014
c5a2d50
Merge pull request #2175 from BrickXu/fix_2171
lovelydinosaur Dec 3, 2014
74a9ece
Update build status icon on github pages page.
eiriksm Dec 3, 2014
5f2f54b
Merge pull request #2188 from eiriksm/patch-2
lovelydinosaur Dec 3, 2014
f2dd05a
Improved nested update test in update().
lovelydinosaur Dec 3, 2014
e1d98f7
Improve nested update and create testing.
lovelydinosaur Dec 3, 2014
ab25d70
Renamed validated_attrs to validated_data to be more in line with oth…
mtschammer Dec 3, 2014
66bce38
Merge pull request #2196 from tomchristie/tomchristie-improve-update-…
kevin-brown Dec 3, 2014
f221b73
Merge pull request #2197 from mtschammer/mtschammer-validated_attrs-r…
lovelydinosaur Dec 3, 2014
e8cbf41
Merge pull request #2172 from kchange/master
lovelydinosaur Dec 4, 2014
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Minor tweaks for helpful message on Model.objects.create() failure.
  • Loading branch information
lovelydinosaur committed Dec 2, 2014
commit 76ac641fbd6c9d7dff5da3c551c3fd1ef7dedd2e
23 changes: 14 additions & 9 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
)
import copy
import inspect
import sys
import warnings

# Note: We do the following so that users of the framework can use this style:
Expand Down Expand Up @@ -658,14 +657,20 @@ def create(self, validated_attrs):
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.\nOriginal 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:
Expand Down
10 changes: 3 additions & 7 deletions tests/test_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from django.db import models
from django.test import TestCase
from rest_framework import serializers
import pytest


def dedent(blocktext):
Expand Down Expand Up @@ -87,13 +86,10 @@ class Meta:
'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):
Expand Down