繁体   English   中英

django-mptt显示“无此列”

[英]django-mptt show “no such column”

我使用MPTT实施了一个论坛,该论坛允许发布最新新闻。我无法解决此错误:“无此列: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

谁能给我一些解决办法的建议?谢谢〜

MPTT在使用它的模型中添加了字段-据我python manage.py makemigrations <your app> && python manage.py migrate ,应该足以运行python manage.py makemigrations <your app> && python manage.py migrate MTPP教程在“ 设置模型 ”部分中提到了这一点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM