django-models - 在 Django Polls 教程中:获取“NameError: name 'Question' is not defined”
问题描述
已解决:我需要在 shell 中导入对象。
>>> from polls.models import Choice, Question # Import the model classes we just wrote.
运行命令Question.objects.all()
返回错误NameError: name 'Question' is not defined.
我的 polls/models.py 看起来像这样:
from django.db import models
from django.utils import timezone
import datetime
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE)
choice_text = models.CharField(max_length=200)
votes = models.IntegerField(default=0)
令人费解的是,我为本教程的第一个片段运行了 shell 并且没有任何问题。我能够创建一个 Question 对象并对其进行查询并完成所有预期的事情。我对 models.py 进行了一些编辑(添加了时区和str(因为已注释掉并在此处删除)),现在根本无法识别 Question 类。
这是怎么回事?
谢谢你的帮助。
解决方案
from polls.models import Choice, Question # 导入我们刚刚写的模型类。
推荐阅读
- css - CSS 属性 cursor:pointer 在一定的窗口宽度下工作,同时在其他窗口宽度上消失
- c# - @Html.EditorFor asp.net 核心不适用于文件
- node.js - Uncaught SyntaxError: Unexpected token < for GET requests in production mode
- r - dplyr摘要r中的NA变量
- c# - 将 IWrapTestMethod 属性应用于整个夹具?
- javascript - Prevent content jumping on position change to fixed
- javascript - 单元测试角度@Input设置功能未触发
- ios - 如何唯一识别设备,每次安装应用程序时 UUID 都会发生变化
- php - 用 AJAX 显示表,选择行,在其他表中显示行,保存到数据库中
- jquery - Kendo UI 图,SVG 滚动条和高度问题