Python django';s测试
我在django模型中有一个class方法,它使用Python django';s测试,python,json,django,postgresql,Python,Json,Django,Postgresql,我在django模型中有一个class方法,它使用json\u array\u elements函数 当它通过浏览器执行时,工作正常。 但在测试中它失败了。 python manage.py测试 Traceback (most recent call last): File "path_to_project/dj_server/model_animations/tests.py", line 94, in test_cteating response_first = model_an
json\u array\u elements
函数
当它通过浏览器执行时,工作正常。
但在测试中它失败了。
python manage.py测试
Traceback (most recent call last):
File "path_to_project/dj_server/model_animations/tests.py", line 94, in test_cteating
response_first = model_animations.views.get_animations_list(request, groupid)
File "path_to_project/dj_server/model_animations/views.py", line 37, in get_animations_list
for model_anim in listArray:
File "/Library/Python/2.7/site-packages/django/db/models/query.py", line 1535, in __iter__
query = iter(self.query)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 76, in __iter__
self._execute_query()
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py", line 90, in _execute_query
self.cursor.execute(self.sql, self.params)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/Library/Python/2.7/site-packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/Library/Python/2.7/site-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
ProgrammingError: function json_array_elements(text) does not exist
LINE 1: ...on_name FROM model_animations_model, json_array...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
在models.py中
@classmethod
def animations_list(self, group_id):
if group_id:
try:
listArray = ModelAnimationList.objects.raw(('SELECT * FROM model_animations_model, json_array_elements(animated_groups) AS data WHERE \'"%s"\' = data::text' %group_id))
return listArray
except:
pass
return None
request = HttpRequest()
response_first = model_animations.views.get_animations_list(request, groupid)
在视图中.py
def获取动画列表(请求、组id):
在测试中。py
@classmethod
def animations_list(self, group_id):
if group_id:
try:
listArray = ModelAnimationList.objects.raw(('SELECT * FROM model_animations_model, json_array_elements(animated_groups) AS data WHERE \'"%s"\' = data::text' %group_id))
return listArray
except:
pass
return None
request = HttpRequest()
response_first = model_animations.views.get_animations_list(request, groupid)
已安装:python 2.7
Django 1.7.1
博士后9.3.5
psycopg2 2.5.4
Mac 10.10优胜美地查询错误
json\u数组\u元素(动画组)
需要对此进行更改:
json\u数组元素(动画组::json)
Postgres json处理函数,例如json\u数组元素(动画组::json)
,jsonb\u数组元素(动画组::jsonb)
将采用json
或jsonb
值
因为postgres JSON字段(动画组
)存储为文本格式或二进制格式。
因此,我们需要使用::json
或::jsonb
转换类型,它给出了json或jsonb格式
检查您能为失败的测试用例发布代码吗?哦,代码太多了。我会尽量把事情简单化。