- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我定义了以下模型。
class Response(models.Model):
question = models.ForeignKey(Question, on_delete = models.CASCADE)
STATEMENT_CHOICES = (
(-1, "Strongly Disagree"),
(-0.5, "Somewhat Disagree"),
(0, "Uncertain"),
(0.5, "Somewhat Agree"),
(1, "Strongly Agree"),
)
statement = models.DecimalField(
max_digits=2,
decimal_places=1,
choices = STATEMENT_CHOICES
)
def __str__(self):
return self.statement
当我运行 django shell 时,我创建一个问题 q
,表示 you like Pizza
和响应 r
,即 1
表示强烈同意
。然后,当我在 r
上运行 __str__
方法时,出现错误,提示 name 'statement' is not Define
。
这是确切的控制台输出...
>>> q
<Question: You like pizza.>
>>> r = Response(question=q, statement=1)
>>> r.question
<Question: You like pizza.>
>>> r.statement
1
>>> r
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/Users/TN/Documents/Programming/Django/Practice/myve /lib/python3.5/site-packages/django/db/models/base.py", line 572, in __repr__
u = six.text_type(self)
File "/Users/TN/Documents/Programming/Django/Practice/mysite/personalityQuiz/models.py", line 48, in __str__
return self.statement
NameError: name 'statement' is not defined
最佳答案
使用self.get_statement_display()
返回选项的描述。请参阅the docs了解更多信息。
关于python - Django - __str__ 方法不起作用,表示名称未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47538602/
在 Python 3 中有没有一种方法可以以编程方式递归调用私有(private)成员变量唯一的 __str__ 函数?像这样的东西: class A: def __str__(self):
我正在用 Python 构建一个简单的类。我已经定义了我自己的 __str__ 方法,当我在类的一个实例上调用 print 时,它应该能很好地工作。当我创建该类的实例并对其调用 print 时,出现错
我一直在尝试创建一个理性类,并添加一个 __str__ dunder 方法返回 p(分子)除以 q(分母)。当我尝试打印对象 Rational_1 时,它只提供 init 方法的输出,并且不返回 __
当我尝试 __str__() 国外的方法它提供这个错误 __str__ returned non-string (type Product) 模型.py class Product(models.Mo
使用这样的嵌套模型: class Gov(models.Model): name = models.CharField(max_length=20) def __str__(self)
在 django __str__ 中有没有办法一次输出所有属性,而不是像我在这里做的那样一个一个输出?我计划拥有大约 100 个属性,因此将所有属性都放在 str 方法中似乎不正确。 class Ca
可以 __str__从字面上返回任何我的灵魂想要的东西? __str__ 上的文档状态: [...] the “informal” or nicely printable string represe
我在容器中打印用户定义的类实例时遇到了一些问题。简而言之,如果我的代码是: class A(): def __str__(self): return 'abc' class B
我是 Python 新手,但我一直遇到与此类似的问题 thread 我当前正在运行: Python 3.6.7 海湾合作委员会8.2.0 没有 IDE,只有普通的 *.py 文件 这是我的类(clas
您好,当我尝试打印我的对象时,我遇到了 __str__ 问题。解释器告诉我“TypeError:格式字符串的参数不足” 这是我正在尝试运行的代码! 'My Practice Class'
我正在尝试改进您在 codecademy python 类(class)中制作的“战舰”游戏,我决定我的第一步是实现类。 我预期的代码输出是五行 O O O O O。当我在 for 循环中只使用一个
这个问题在这里已经有了答案: functools.partial on class method (2 个答案) 关闭 5 年前。 我在尝试为一个程序编写 pretty-print 程序时遇到了这个
这是我的脚本: import math class Vector: def __init__(self, x=0.0, y=0.0): self.x = x s
我正在开发一个绘制线并找到中点的模块。出于测试目的,我想从相关类创建一些字符串输出。 class Line: def __init__(self, endpoints): self.sta
假设我有一个导入的模块,它工作正常但没有提供好的 __str__ 方法,我想添加一个。 在一些混合语言中: from foo import Foo Foo.prototype._str__ = fun
我了解到 __str__可以定义对象字符串的输出。 例子: class Person(object): def __init__(self, name): self.name
这个问题在这里已经有了答案: Why can't I replace the __str__ method of a Python object with another function? (2
在引发异常后,我试图覆盖 Python 中 Exception 子类的打印输出,但我没有运气让我的覆盖实际被调用。 def str_override(self): """ Overri
我想将 python 函数的字符串表示更改为函数名称。 例如某些功能 def blah(x): ... str(blah) 当前给出 所以我这样替换 __str__: blah.__str__=l
谁能告诉我 print sth 和 print str(sth) 之间的区别? 例如在 official documentation for sqlite3 的示例中,目前可以看到以下创建数据库的代码
我是一名优秀的程序员,十分优秀!