2014-11-14 61 views
0

我在django模型中使用了json_array_elements函數的類方法。Postgres函數json_array_elements在django的測試中沒有找到

在通過瀏覽器執行的情況下,它工作正常。 但在測試中失敗。 python manage.py test

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 

在views.py DEF get_animations_list(請求,GROUP_ID): ...在

listArray = ModelAnimationList.animations_list(group_id) 
if listArray: 
    for model_anim in listArray: 
     if model_anim: 
      anim_dict = { 'a_id' : model_anim.id, 'a_name' : model_anim.animation_name } 
      result_anim_list.append(anim_dict) 

... 

試驗。 PY

request = HttpRequest() 
response_first = model_animations.views.get_animations_list(request, groupid) 

安裝:
蟒蛇2.7
的Django 1.7.1
的Postgres 9.3.5
psycopg2 2.5.4
的Mac 10.10約塞米蒂

+0

您能否爲您失敗的測試用例發佈代碼? – adds68 2014-11-14 14:57:05

+0

哦,這是很多代碼。我會嘗試做一些簡單的事情。 – SAKrisT 2014-11-14 16:37:17

回答

6

錯誤查詢json_array_elements(animated_groups)

需要變更爲: json_array_elements(animated_groups::json)