Python/django 新手。我有一个我创建的模型“评论”。我有一个想要创建新评论模型的 View 。当我这样做时(单击提交按钮((实际上是“创建”按钮))),我得到:
ProgrammingError at /reviews/ (1146, "Table 'my_app.my_app_comment' doesn't
exist")
模型.py
class Comments(models.Model):
comment_text = models.CharField(max_length=600, default="None")
recipe = models.ForeignKey('cookbook.BaseRecipe', related_name='val1')
View .py
class CommentCreate(CreateView):
model = Comments
fields = ['comment_text']
template_name = 'commentcreate.html'
success_url = 'commentcreate.html'
url.py
urlpatterns = [
url(r'^$', CommentCreate.as_view(), name='comment')
]
commentcreate.html 文件的一部分
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create" />
</form>
我认为这张表(它说缺少的那个)将由 django 自动创建。
最佳答案
您需要为新模型应用迁移。在项目的文件夹中,运行
manage.py makemigrations [application_name]
manage.py migrate [application_name]
这里的[application_name]
是可选的(您可以将其替换为实际修改后的Django应用程序的名称,或者省略它)。
如果您使用的是 Python3,则可能需要运行 python3 manage.py ...
,而不仅仅是 manage.py ...
。
关于python - 我不明白为什么我的数据库没有更新新模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35911134/