簡體   English   中英

操作錯誤 - 沒有這樣的表 - Django,mptt

[英]operational error - no such table - Django, mptt

我在Django遇到了一個與DB有關的問題,我不明白。

我定義了一個MPTT模型:

class Image(MPTTModel):
    name = models.CharField(max_length=50)
    parent = TreeForeignKey('self', null=True, blank=True, related_name='children')

    def __unicode__(self):  
        return self.name

    def rank(self):         leaves = self.get_leafnodes()       if leaves:          rank = leaves[0].get_level() - self.get_level()         else:           rank = 0        return rank

mptt.register(Image, order_insertion_by=['name'])

然后在我的視圖中,我嘗試使用模型的一些語句,並得到一個OperationalError。

def index(request):

    if request.method == 'POST': 
        image_string = request.POST.get('get_image')
        index = image_string.find('(')
        if index == -1:
            parent = image_string
            child = None
        else:
            parent = image_string[0:index]
            child = image_string[index+1:len(image_string)-1]       
        try:
            images = Image.objects.all()
            image_names = [a.name for a in images]     
        except Image.DoesNotExist:
            return render(request, 'images_app/index.html', {'images':[]}) 
        else:
            parent_model = Image(name=parent)
            parent_model.save()
            child_model = Image(name=child, parent=parent_model)
            child_model.save()              
            return render(request, 'images_app/index.html', {'images':images})

我不確定這是我的觀點或我定義模型的方式的問題。 根據我的理解,'try'表達式應該使得如果代碼沒有評估,它將簡單地跳到異常。 為什么這不是直接的例外?

Traceback:
File "C:\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
  114.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\djangoprojects\images\images_app\views.py" in index
  17.             if images:
File "C:\Python27\lib\site-packages\django\db\models\query.py" in __nonzero__
  100.         self._fetch_all()
File "C:\Python27\lib\site-packages\django\db\models\query.py" in _fetch_all
  854.             self._result_cache = list(self.iterator())
File "C:\Python27\lib\site-packages\django\db\models\query.py" in iterator
  220.         for row in compiler.results_iter():
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in results_iter
  709.         for rows in self.execute_sql(MULTI):
File "C:\Python27\lib\site-packages\django\db\models\sql\compiler.py" in execute_sql
  782.         cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  69.             return super(CursorDebugWrapper, self).execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  53.                 return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\utils.py" in __exit__
  99.                 six.reraise(dj_exc_type, dj_exc_value, traceback)
File "C:\Python27\lib\site-packages\django\db\backends\util.py" in execute
  53.                 return self.cursor.execute(sql, params)
File "C:\Python27\lib\site-packages\django\db\backends\sqlite3\base.py" in execute
  450.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /images/
Exception Value: no such table: images_app_image

通過查看異常值( no such table: images_app_image ),我猜想實際的數據庫表不存在。

使用./manage dbshell命令檢查數據庫中是否存在該表。 您可以使用命令.schema列出shell中數據庫中的所有表,或使用.schema images_app_image僅顯示實際表的模式定義。

如果該表不存在,請使用./manage syncdb創建它(如果使用South,則使用migrate命令)。

試試python manage.py syncdb

如果你得到

未知命令:'syncdb'

你可以試試

python manage.py migrate --run-syncdb

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM