Skip to content

Improve ModelSerializer.create() error message. #6112

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 4 additions & 2 deletions rest_framework/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,15 +941,17 @@ def create(self, validated_data):
except TypeError:
tb = traceback.format_exc()
msg = (
'Got a `TypeError` when calling `%s._default_manager.create()`. '
'Got a `TypeError` when calling `%s.%s.create()`. '
'This may be because you have a writable field on the '
'serializer class that is not a valid argument to '
'`%s._default_manager.create()`. You may need to make the field '
'`%s.%s.create()`. You may need to make the field '
'read-only, or override the %s.create() method to handle '
'this correctly.\nOriginal exception was:\n %s' %
(
ModelClass.__name__,
ModelClass._default_manager.name,
ModelClass.__name__,
ModelClass._default_manager.name,
self.__class__.__name__,
tb
)
Expand Down
36 changes: 21 additions & 15 deletions tests/test_model_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,6 @@ class Issue3674ChildModel(models.Model):
value = models.CharField(primary_key=True, max_length=64)


class Issue6110TestModel(models.Model):
"""Model without .objects manager."""

name = models.CharField(max_length=64)
all_objects = models.Manager()


class UniqueChoiceModel(models.Model):
CHOICES = (
('choice1', 'choice 1'),
Expand All @@ -133,7 +126,7 @@ class Meta:
})
serializer.is_valid()

msginitial = 'Got a `TypeError` when calling `OneFieldModel._default_manager.create()`.'
msginitial = 'Got a `TypeError` when calling `OneFieldModel.objects.create()`.'
with self.assertRaisesMessage(TypeError, msginitial):
serializer.save()

Expand Down Expand Up @@ -1233,13 +1226,26 @@ class Meta:
self.assertEqual(unicode_repr(TestSerializer()), expected)


class Issue6110Test(TestCase):
def test_model_serializer_custom_manager(self):
class Issue6110TestModel(models.Model):
"""Model without .objects manager."""

class TestModelSerializer(serializers.ModelSerializer):
class Meta:
model = Issue6110TestModel
fields = ('name',)
name = models.CharField(max_length=64)
all_objects = models.Manager()


class Issue6110ModelSerializer(serializers.ModelSerializer):
class Meta:
model = Issue6110TestModel
fields = ('name',)

instance = TestModelSerializer().create({'name': 'test_name'})

class Issue6110Test(TestCase):

def test_model_serializer_custom_manager(self):
instance = Issue6110ModelSerializer().create({'name': 'test_name'})
self.assertEqual(instance.name, 'test_name')

def test_model_serializer_custom_manager_error_message(self):
msginitial = ('Got a `TypeError` when calling `Issue6110TestModel.all_objects.create()`.')
with self.assertRaisesMessage(TypeError, msginitial):
Issue6110ModelSerializer().create({'wrong_param': 'wrong_param'})
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy