Django因异常值崩溃:没有这样的表:polls_question_page [英] Django crashes with Exception Value: no such table: polls_question_page
问题描述
尝试添加许多字段时出现错误:
I get an error when I try to add a many to many fields :
page = models.ManyToManyField(Page)
我收到此错误:没有这样的表:polls_question_page
class Page(models.Model):
title = models.CharField(max_length=30)
def __str__(self):
return self.title
class Question(models.Model):
label = models.CharField(max_length=30)
page = models.ManyToManyField(Page)
def __str__(self):
return self.label
class Reply(models.Model):
question = models.ForeignKey(Question)
user = models.ForeignKey(Personne)
answer = models.CharField(max_length=30)
creationDate = models.DateTimeField(default=timezone.now(),blank=True, verbose_name="Date de création")
def __str__(self):
return str(self.answer)
跟踪:
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py" in execute
323. return Database.Cursor.execute(self, query, params)
The above exception (no such table: polls_question_page) was the direct cause of the following exception:
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
149. response = self.process_exception_by_middleware(e, request)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/core/handlers/base.py" in get_response
147. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/contrib/admin/options.py" in wrapper
541. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
57. response = view_func(request, *args, **kwargs)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/contrib/admin/sites.py" in inner
244. return view(request, *args, **kwargs)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/contrib/admin/options.py" in change_view
1438. return self.changeform_view(request, object_id, form_url, extra_context)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapper
67. return bound_func(*args, **kwargs)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/utils/decorators.py" in _wrapped_view
149. response = view_func(request, *args, **kwargs)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/utils/decorators.py" in bound_func
63. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/usr/lib/python3.4/contextlib.py" in inner
30. return func(*args, **kwds)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/contrib/admin/options.py" in changeform_view
1395. form = ModelForm(instance=obj)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/forms/models.py" in __init__
282. object_data = model_to_dict(instance, opts.fields, opts.exclude)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/forms/models.py" in model_to_dict
105. data[f.name] = list(qs.values_list('pk', flat=True))
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/models/query.py" in __iter__
258. self._fetch_all()
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/models/query.py" in _fetch_all
1074. self._result_cache = list(self.iterator())
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/models/query.py" in __iter__
158. for row in compiler.results_iter():
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in results_iter
802. results = self.execute_sql(MULTI)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/models/sql/compiler.py" in execute_sql
848. cursor.execute(sql, params)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
79. return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/utils.py" in __exit__
95. six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/utils/six.py" in reraise
685. raise value.with_traceback(tb)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/backends/utils.py" in execute
64. return self.cursor.execute(sql, params)
File "/home/nicolas/webtrial/dev/lib/python3.4/site-packages/django/db/backends/sqlite3/base.py" in execute
323. return Database.Cursor.execute(self, query, params)
Exception Type: OperationalError at /admin/polls/question/4/change/
Exception Value: no such table: polls_question_page
为什么我不能在班级中添加字段页面"?为什么我无法管理我的管理员当我删除此字段时,我担心会在管理面板中看到类问题,但我会在页面上添加ManyToMany键...
Why can not I add my fields "page" in my class Question? Why I do not manage to access my admin When I remove this fields are any worries to see the class question in my admin panel but I would add a ManyToMany key on page ...
我要加入另一个人吗?
当我运行时: python manage.py makemigrations
/home/nicolas/webtrial/dev/lib/python3.4/site-packages/grappelli/urls.py:21: RemovedInDjango110Warning: django.conf.urls.patterns() is deprecated and will be removed in Django 1.10. Update your urlpatterns to be a list of django.conf.urls.url() instances instead.
url(r'^switch/user/(?P<object_id>\d+)/$', switch_user, name="grp_switch_user"),
System check identified some issues:
WARNINGS:
polls.Message.pub_date: (fields.W161) Fixed default value provided.
HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
polls.Reply.creationDate: (fields.W161) Fixed default value provided.
HINT: It seems you set a fixed date / time / datetime value as default for this field. This may not be what you want. If you want to have the current date as default, use `django.utils.timezone.now`
No changes detected
推荐答案
这意味着django模型与您的数据库不同步,因此您必须进行并运行迁移.
This means that the django model and your database are not in sync, and you have to make and run migrations.
Exception Value: no such table: polls_question_page
运行以下管理命令:
manage.py makemigrations && manage.py migrate
如果这样不起作用,则必须修复数据库
获取应该已应用迁移的列表
Get a list of which migrations is supposed to have been applied
manage.py showmigrations
通读应用程序"polls"的迁移源,找出哪些尚未应用.然后,您可以回滚到迁移之前.假设您要回滚到名为"0004_something_something"的迁移
Read through the source of the migrations for the app "polls" and find out which that hasn't been applied. Then you can roll back to before the migration. Let's say that you want to roll back to a migration called "0004_something_something"
manage.py migrate polls 0004
如果有错误,您可以将其伪造.
manage.py migrate polls 0004 --fake
然后重新运行迁移.
manage.py migrate polls
希望这可以解决所有问题.如果没有,您可能只想从一个空的数据库重新开始.
Hopefully this will fix everything. If not, you might want to just start over with an empty database.
最简单的完全重置数据库的方法可能是软件包django-extensions .
The easiest way to reset your database completely is probably the command reset_db
from the package django-extensions.
reset_db -重置数据库(当前为sqlite3,mysql,postgres).使用"DROP DATABASE"和"CREATE DATABASE".
reset_db - Resets a database (currently sqlite3, mysql, postgres). Uses "DROP DATABASE" and "CREATE DATABASE".
这篇关于Django因异常值崩溃:没有这样的表:polls_question_page的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!