【问题标题】:Django-comments and legacy database? "OperationalError: no such table"Django 评论和遗留数据库? “操作错误:没有这样的表”
【发布时间】:2015-11-01 19:36:31
【问题描述】:

我正在使用legacy database(除了“普通”数据库),在我的settings.py 中定义:

DATABASES = {
    'default': {
         'ENGINE': 'django.db.backends.sqlite3',
         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
                 },
    'articles_database': {
         'ENGINE': 'django.db.backends.sqlite3',
         'NAME': os.path.join(BASE_DIR, 'articles.db'),
 } }

在我的models.py 中:

from django.db import models

class ArticlesTable(models.Model):
    id = models.IntegerField(primary_key=True)  # AutoField?
    article_type = models.TextField(blank=True, null=True)  # This field type is a guess.
    article_name = models.TextField(blank=True, null=True)  # This field type is a guess.
    article_number = models.IntegerField(db_column='article_number', blank=True, null=True)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'articles_table'

在我的 Django 1.8.5 上安装 Django-cmets 后:我可以得到一个正确的表格来填写评论,但是单击“发布”按钮会出现此错误:

OperationalError at /comments/post/

no such table: articles_table 

突出显示错误行:

/home/gus/.Envs/python3_django/lib/python3.4/site-packages/django_comments/views/comments.py in post_comment

56. target = model._default_manager.using(using).get(pk=object_pk)

显然,Django-cmets 没有在我的数据库中找到我的表?实际上是否可以在 Django-cmets 中使用旧数据库?

编辑:我已经按照@Geo Jacob 的建议修复了旧数据库的模型:

class Meta:
    managed = True
    db_table = 'articles_table'

但是现在我得到了一个错误页面(由 Django-cmets 提供,不是 Debug 页面):

Comment post not allowed (400)
Why: No object matching content-type 'articles.articlestable' and 
object PK '1' exists.

The comment you tried to post to this view wasn't saved because 
something tampered with the security information in the comment 
form. The message above should explain the problem, or you can check 
the comment documentation for more help.

post_comment 函数中的 Django-cmets 获得了正确的模型 (ArticlesTable) 但找不到对象???

【问题讨论】:

    标签: django django-comments legacy-database


    【解决方案1】:

    你必须删除managed=False

    class Meta:
        managed = True
        db_table = 'articles_table'
    

    现在makemigrations或syncdb,然后articles_table将被创建。

    【讨论】:

    • 现在我得到:不允许发表评论 (400) 原因:不存在与内容类型“articles.articlestable”和对象 PK“1”匹配的对象。您尝试发布到此视图的评论未保存,因为评论表单中的安全信息被篡改了。
    猜你喜欢
    • 2015-07-31
    • 2013-07-13
    • 2014-03-26
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 2014-01-06
    • 1970-01-01
    相关资源
    最近更新 更多