【发布时间】:2016-06-25 10:33:48
【问题描述】:
我有一个使用 Django Rest Framework 3.3.2 和 Python 3.4 的 Django 1.8.9 应用程序
我愿意测试我的 API 是否正常运行。
我陷入了一项测试,该测试旨在验证我是否可以通过 API 将数据发布到模型中。问题是:这个模型与另一个模型有外键关系。这个外键被 HyperlinkedModelSerializer 转换成超链接。
例如:http://host:port/api/relation-model-name/id-related-model
这是我的测试:(重要的部分是 data={...})
def test_api_create(self):
""" Test the creation of new entries via the API """
url = reverse(self.api_model_url+'-list')
data = {'sid':'New change entry', 'status':'THE HYPERLINK TO THE RELATED MODEL SHOULD BE HERE', 'name':'New change entry'}
# Check that a new entry can be created by an administrator via the API
self.api_client.login(username='admin', password='admin')
response = self.api_client.post(url, data, format='json')
content = self.parse_json_response(response, Status.HTTP_201_CREATED)
self.assertEqual(content['sid'], 'New change entry')
如果我硬写超链接但我想以通用方式构建它,则此测试有效。 我可以轻松检索相关字段的 id。我想在我的测试中获取超链接。
提前感谢您的帮助!
干杯!
【问题讨论】:
标签: python django rest python-3.x django-rest-framework