Update serializers.py

This commit is contained in:
orf 2015-10-16 09:50:15 +01:00
parent 1b4a41cb80
commit b5cd438986

View File

@ -13,6 +13,7 @@ response content is handled by parsers and renderers.
from __future__ import unicode_literals from __future__ import unicode_literals
import warnings import warnings
import traceback
from django.db import models from django.db import models
from django.db.models.fields import Field as DjangoModelField from django.db.models.fields import Field as DjangoModelField
@ -844,18 +845,19 @@ class ModelSerializer(Serializer):
try: try:
instance = ModelClass.objects.create(**validated_data) instance = ModelClass.objects.create(**validated_data)
except TypeError as exc: except TypeError as exc:
tb = traceback.format_exc()
msg = ( msg = (
'Got a `TypeError` when calling `%s.objects.create()`. ' 'Got a `TypeError` when calling `%s.objects.create()`. '
'This may be because you have a writable field on the ' 'This may be because you have a writable field on the '
'serializer class that is not a valid argument to ' 'serializer class that is not a valid argument to '
'`%s.objects.create()`. You may need to make the field ' '`%s.objects.create()`. You may need to make the field '
'read-only, or override the %s.create() method to handle ' 'read-only, or override the %s.create() method to handle '
'this correctly.\nOriginal exception text was: %s.' % 'this correctly.\nOriginal exception was:\n %s.' %
( (
ModelClass.__name__, ModelClass.__name__,
ModelClass.__name__, ModelClass.__name__,
self.__class__.__name__, self.__class__.__name__,
exc tb
) )
) )
raise TypeError(msg) raise TypeError(msg)