Python Django REST API在保存模型时测试失败,但运行良好

Python Django REST API在保存模型时测试失败,但运行良好,python,django,rest,unit-testing,Python,Django,Rest,Unit Testing,我对此很迷茫。我有一个来自Django的REST框架的ModelViewSet,它通过覆盖perform\u create方法对POST请求执行一些自定义功能。看起来是这样的: class UserViewSet(viewsets.ModelViewSet): queryset = User.objects.all() serializer_class = UserFlat def perform_create(self, serializer):

我对此很迷茫。我有一个来自Django的REST框架的ModelViewSet,它通过覆盖
perform\u create
方法对POST请求执行一些自定义功能。看起来是这样的:

class UserViewSet(viewsets.ModelViewSet):
    queryset         = User.objects.all()
    serializer_class = UserFlat

    def perform_create(self, serializer):
         serializer.save()

         try:
             profile_country = self.request.data['country']
             profile_phone = self.request.data['phone']

             profile = Profile.objects.get(user_id=serializer.data['id'])
             country = Country.objects.get(country_code=profile_country)

             # both of these return the object just fine
             # print profile
             # print country

             # Take the profile object and set these attributes. It has a ForeignKey relation with the country
             profile.phone  = profile_phone
             profile.origin = country

             # This is where Django's TestCase fails
             profile.save()

         except:
             print "Attributes not provided"
因此,我在我的终端上用
httpie
测试了上述内容,结果显示一切正常。当我去检查数据库时,所有的信息都在那里。创建了用户对象,并使用国家/地区和电话号码的外键创建了新的配置文件对象(我使用post_保存信号创建配置文件对象)

但是,当我运行测试时,它在
profile.save()部分失败。我已经确认Country对象是在测试之前在DB中创建的,如果我在运行测试时打印出来,我可以看到它清楚地拾取了所有相关信息

http POST:8000/api/v1/users/username=test password=test last\u name=test first\u name=test email=test@test.com国家/地区=美国电话=555

上面的行在我的终端中运行,并成功地发布到我的数据库

下面是失败的测试用例

class APITestCase(TransactionTestCase):
    def test_create_user_profile_through_api(self):
    url = '/api/v1/users/'

    # country = Country.objects.get(country_code='US')

    data = {
        'first_name':'Tom',
        'last_name':'Sawyer',
        'username':'tomsawyer',
        'password':'tomsawyer',
        'email':'tomsawyer@tomsawyer.com',
        'country': 'US',
        'phone':'5555555555'
    }

    # The test fails to save the additional profile information profile.save() within
    # the view's perform_create method. Why is that?
    post = self.client.post(url, data, format='json')

    # request = self.client.get('/api/v1/users/3')
    # Profile.objects.get(id=3) 
    # both lines above are me checking to see if the profile object showed
    # up with the relevant country and phone number fields. Fields are 
    # are empty although the profile object is there

    # I didn't write the assertion yet as the line above keeps coming up empty
当我运行测试时,视图中的except块出现
“Attributes not provided”

编辑: 另一方面,我检查了身份验证之类的东西,甚至通过REST框架将其关闭/强制,这一切都很好。我能够在没有API的情况下成功地从API发布和检索所有其他场景

编辑:回溯

  File "app/api/views.py", line 86, in perform_create
profile.save()

 File "venv/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
 return self.cursor.execute(sql, params)
DataError: integer out of range

首先去掉有害的
except
子句-它所做的只是阻止您获取有关出错原因的相关信息(异常类型、消息和完整回溯),然后回到这里发布这些信息(但很有可能到那时您已经知道了足够的信息,可以自己解决问题).我不知道该怎么办。回溯显示第86行(profile.save())并显示
DataError:Integer超出范围
。我已经用回溯更新了上面的内容运行测试时是否存在匹配的配置文件和国家/地区实例?就像在中一样,您是否将它们作为固定装置或通过其他方式加载。这当然不是完整的回溯-在调用
profile.save()
和django/db/backends/utils.py之间还有许多其他堆栈帧。也只是出于好奇:你没有使用
models.IntegerField
来存储电话号码,是吗?哈哈,对不起,我是新手,我不知道其他所有的线路是什么意思。是的,我确实使用了
模型。IntegerField
。实际上,我只是把它注释掉了,它工作得很好,但我不知道为什么它停止了。我试着在电话号码上使用
int()。我的模型字段定义为
models.IntegerField(max_length=15,blank=True,null=True)