我的任务是将一些代码从python 2.7更新到python 3.6
目前代码中断了:
TypeError:'map'对象不可订阅
原始代码:
def test_create_page(self):
"""Ensure we can make a page"""
response = DispatchTestHelpers.create_page(self.client)
id = response.data['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
try:
page = Page.objects.get(pk=response.data['id'])
except Page.DoesNotExist:
self.fail('The page should exist in the database')
print()
print()
print(response.data)
print()
print()
# Check Data
self.assertEqual(response.data['title'], 'Test Page')
self.assertEqual(response.data['slug'], 'test-page')
self.assertEqual(response.data['snippet'], 'This is a test snippet')
self.assertEqual(response.data['content'][0]['type'], 'paragraph')
self.assertEqual(response.data['content'][0]['data'], 'This is some paragraph text')
原始输出:
======================================================================
ERROR: test_create_page (dispatch.tests.test_api_pages.PagesTest)
Ensure we can make a page
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/icenine/code/ubyssey-
dev3/dispatch/dispatch/tests/test_api_pages.py", line 67, in
test_create_page
self.assertEqual(response.data['content'][0]['type'], 'paragraph')
TypeError: 'map' object is not subscriptable
到目前为止,我已经通过将地图对象转换为列表来解决了这个问题,但是这会返回一个空列表。据我所知,地图对象在被列为列表之前没有被任何方式触及,如https://stackoverflow.com/a/45018536/6448060所述。
从以下函数返回字典:
@classmethod
def create_page(cls, client, title='Test Page', slug='test-page'):
"""Create dummy page"""
url = reverse('api-pages-list')
data = {
'title': title,
'slug': slug,
'snippet': 'This is a test snippet',
'content': [
{
'type': 'paragraph',
'data': 'This is some paragraph text'
}
]
}
return client.post(url, data, format='json')
尝试解决方案:
def test_create_page(self):
"""Ensure we can make a page"""
response = DispatchTestHelpers.create_page(self.client)
id = response.data['id']
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
try:
page = Page.objects.get(pk=response.data['id'])
except Page.DoesNotExist:
self.fail('The page should exist in the database')
data_list = list(response.data['content'])
print()
print(response.data)
print()
print(data_list)
print()
# Check Data
self.assertEqual(response.data['title'], 'Test Page')
self.assertEqual(response.data['slug'], 'test-page')
self.assertEqual(response.data['snippet'], 'This is a test snippet')
self.assertEqual(data_list[0]['type'], 'paragraph')
self.assertEqual(data_list[0]['data'], 'This is some paragraph text')
尝试解决方案输出:
{'id': 1, 'slug': 'test-page', 'title': 'Test Page', 'featured_image': None, 'snippet': 'This is a test snippet', 'content': <map object at 0x7ff15f04e048>, 'published_at': None, 'is_published': False, 'published_version': None, 'current_version': 1, 'latest_version': 1, 'preview_id': '8720814f-a5e8-4892-b592-8cbb4d0d019f', 'template': OrderedDict([('id', 'default'), ('name', 'Default')]), 'template_data': {}, 'seo_keyword': None, 'seo_description': None}
[]
======================================================================
ERROR: test_create_page (dispatch.tests.test_api_pages.PagesTest)
Ensure we can make a page
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/icenine/code/ubyssey-
dev3/dispatch/dispatch/tests/test_api_pages.py", line 77, in
test_create_page
self.assertEqual(data_list[0]['type'], 'paragraph')
IndexError: list index out of range
正如您在第一行中看到的那样,'content'键的值是一个地图对象。
如何从返回的字典中访问列表值?
答案 0 :(得分:0)
response.data['content'][0]['type']
我认为response.data['content']
不是列表,因此尝试访问其第0个元素失败。
'content': <map object at 0x7ff15f04e048>,
查找该map
对象的文档;它可能在你用来实际点击你的URL的库的文档中。
答案 1 :(得分:0)
所以问题出现在几个与django相关的模块文件中。有几个函数返回了地图类型,一个用于转换随地图返回的json数据。我用这样的列表包装了返回的地图
return list(map(<whatever is being mapped>))
现在的问题似乎已经解决了。