File tree Expand file tree Collapse file tree 2 files changed +17
-16
lines changed Expand file tree Collapse file tree 2 files changed +17
-16
lines changed Original file line number Diff line number Diff line change 35
35
)
36
36
import copy
37
37
import inspect
38
- import sys
39
38
import warnings
40
39
41
40
# Note: We do the following so that users of the framework can use this style:
@@ -658,14 +657,20 @@ def create(self, validated_attrs):
658
657
instance = ModelClass .objects .create (** validated_attrs )
659
658
except TypeError as exc :
660
659
msg = (
661
- 'The mentioned argument might be a field on the serializer '
662
- 'that is not part of the model. You need to override the '
663
- 'create() method in your ModelSerializer subclass to support '
664
- 'this.' )
665
- six .reraise (
666
- type (exc ),
667
- type (exc )(str (exc ) + '. ' + msg ),
668
- sys .exc_info ()[2 ])
660
+ 'Got a `TypeError` when calling `%s.objects.create()`. '
661
+ 'This may be because you have a writable field on the '
662
+ 'serializer class that is not a valid argument to '
663
+ '`%s.objects.create()`. You may need to make the field '
664
+ 'read-only, or override the %s.create() method to handle '
665
+ 'this correctly.\n Original exception text was: %s.' %
666
+ (
667
+ ModelClass .__name__ ,
668
+ ModelClass .__name__ ,
669
+ self .__class__ .__name__ ,
670
+ exc
671
+ )
672
+ )
673
+ raise TypeError (msg )
669
674
670
675
# Save many-to-many relationships after the instance is created.
671
676
if many_to_many :
Original file line number Diff line number Diff line change 10
10
from django .db import models
11
11
from django .test import TestCase
12
12
from rest_framework import serializers
13
- import pytest
14
13
15
14
16
15
def dedent (blocktext ):
@@ -87,13 +86,10 @@ class Meta:
87
86
'non_model_field' : 'bar' ,
88
87
})
89
88
serializer .is_valid ()
90
- with pytest . raises (TypeError ):
89
+ with self . assertRaises (TypeError ) as excinfo :
91
90
serializer .save ()
92
-
93
- try :
94
- serializer .save ()
95
- except TypeError as exc :
96
- assert 'ModelSerializer' in str (exc )
91
+ msginitial = 'Got a `TypeError` when calling `OneFieldModel.objects.create()`.'
92
+ assert str (excinfo .exception ).startswith (msginitial )
97
93
98
94
99
95
class TestRegularFieldMappings (TestCase ):
You can’t perform that action at this time.
0 commit comments