【问题标题】:django-mptt show "no such column"django-mptt 显示“没有这样的列”
【发布时间】:2016-09-16 05:37:20
【问题描述】:

我使用 MPTT 实现了一个允许发布最新新闻的论坛。我无法解决此错误:“no such column: news_comment.lft”

应用名称是news。这是models.py:

from django.db import models
from mptt.models import MPTTModel, TreeForeignKey

class News(models.Model):
    ....

class Comment(MPTTModel):
    ....
    news    = models.ForeignKey(News,related_name='news', null=True)
    parent  = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True)

    class MPTTMeta:
        order_insertion_by = ['created']

这是views.py

def show_news(request,request_id):
    news = News.objects.get(id=request_id)
    comments = Comment.objects.filter(news=news)

    return render(  request,
                'news.html',
                {'news': news,
                 'comments': comments,})

这是 news.html 的一部分

{% block title %}{{news.title}}{% endblock title %}

{% block content %}
<div id='news_info'>
    <h1>{{ news.title }}</h1>
    <p>{{ news.created}}</p>
    <p>{{ news.content}}</p>
</div>
<div id="comment_list">
    <h2>Comments</h2>
    **{% for comment in comments %} -- error here**
        {% if comment.replyto == null %}
            <div class="comment_info">
             ....

这是错误报告:

Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/news/4/

Django Version: 1.10.1
Python Version: 3.5.2
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'news',
 'mptt']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']


Template error:
In template /home/sinoease/Desktop/ross/Project/NewsPortal/news/templates/news.html, error at line 11
   no such column: news_comment.lft   
   1 : {% block title %}{{news.title}}{% endblock title %}
   2 : 
   3 : {% block content %}
   4 :     <div id='news_info'>
   5 :         <h1>{{ news.title }}</h1>
   6 :         <p>{{ news.created}}</p>
   7 :         <p>{{ news.content}}</p>
   8 :     </div>
   9 :     <div id="comment_list">
   10 :         <h2>Comments</h2>
   11 :          {% for comment in comments %} 
   12 :             {% if comment.replyto == null %}
   13 :                 <div class="comment_info">
   14 :                 <div class="comment_content">
   15 :                     comment info:{{comment.content}}
   16 :                 </div>
   17 :                 <div clss="comment_create">
   18 :                     comment on: {{comment.created}}
   19 :                 </div>
   20 :                 <a href="{% url 'reply_comment' news.id comment.id %}"><button>Reply</button></a>                
   21 : 


Traceback:

File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py" in execute
  337.         return Database.Cursor.execute(self, query, params)

The above exception (no such column: news_comment.lft) was the direct cause of the following exception:

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py" in inner
  39.             response = get_response(request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  187.                 response = self.process_exception_by_middleware(e, request)

File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py" in _get_response
  185.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/sinoease/Desktop/ross/Project/NewsPortal/news/views.py" in show_news
  17.                    'comments': comments,})

File "/usr/local/lib/python3.5/dist-packages/django/shortcuts.py" in render
  30.     content = loader.render_to_string(template_name, context, request, using=using)

File "/usr/local/lib/python3.5/dist-packages/django/template/loader.py" in render_to_string
  68.     return template.render(context, request)

File "/usr/local/lib/python3.5/dist-packages/django/template/backends/django.py" in render
  66.             return self.template.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  208.                     return self._render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in _render
  199.         return self.nodelist.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/loader_tags.py" in render
  61.                 result = self.nodelist.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render
  994.                 bit = node.render_annotated(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/base.py" in render_annotated
  961.             return self.render(context)

File "/usr/local/lib/python3.5/dist-packages/django/template/defaulttags.py" in render
  166.             len_values = len(values)

File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py" in __len__
  238.         self._fetch_all()

File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py" in _fetch_all
  1087.             self._result_cache = list(self.iterator())

File "/usr/local/lib/python3.5/dist-packages/django/db/models/query.py" in __iter__
  54.         results = compiler.execute_sql()

File "/usr/local/lib/python3.5/dist-packages/django/db/models/sql/compiler.py" in execute_sql
  835.             cursor.execute(sql, params)

File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py" in execute
  79.             return super(CursorDebugWrapper, self).execute(sql, params)

File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py" in __exit__
  94.                 six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py" in reraise
  685.             raise value.with_traceback(tb)

File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py" in execute
  64.                 return self.cursor.execute(sql, params)

File "/usr/local/lib/python3.5/dist-packages/django/db/backends/sqlite3/base.py" in execute
  337.         return Database.Cursor.execute(self, query, params)

Exception Type: OperationalError at /news/4/
Exception Value: no such column: news_comment.lft

谁能告诉我如何解决这个问题?谢谢~

【问题讨论】:

  • 堆栈跟踪显示错误位于 {% block title %}{{news.title}}{% endblock title %} 而不是您突出显示的位置。

标签: python django django-mptt


【解决方案1】:

MPTT 向正在使用它的模型添加字段 - 据我所知,运行 python manage.py makemigrations &lt;your app&gt; &amp;&amp; python manage.py migrate 应该足够了。 MTPP Tutorial设置你的模型部分提到了这一点。

【讨论】:

    猜你喜欢
    • 2014-03-26
    • 2021-04-03
    • 2021-11-07
    • 2021-11-13
    • 2015-02-25
    • 2020-08-16
    • 2014-09-23
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多