从hstore字段获取值时遇到问题。情况是这样的:
型号:
class Model(models.Model):
hstoreField = HStoreField()
测试案例:
def test_case_1(self):
new_model = Model.objects.create(
hstoreField = {
'key1': 'value1',
'key2': 'value2'
}
)
self.assertEqual(new_model.subscription['key1'], 'value1')
self.assertEqual(new_model.subscription['key2'], 'value2')
def test_case_2(self):
Model.objects.create(
hstoreField = {
'key1': 'value1',
'key2': 'value2'
}
)
new_model = Model.objects.get( id = 1 ) // assume it works
self.assertEqual(new_model.hstoreField['key1'], 'value1')
self.assertEqual(new_model.hstoreField['key2'], 'value2')
运行这些测试用例时,第一个通过了,但是第二个失败了,抛出的错误是:TypeError: string indices must be integers
。检查hstoreField的值是一个字符串,应该是一个字典。
该错误仅在测试中发生,在实际代码中,两种情况均有效。你能帮我吗?