【发布时间】:2021-06-01 14:24:49
【问题描述】:
我一次又一次地收到此错误,我不知道为什么我尝试了所有方法但似乎不起作用, 我正在尝试将元素添加到我的主页中!
from django.db import models
class ToDo(models.Model):
name = models.CharField(max_length = 200, unique = False)
def __str__(self):
return self.name
class Item(models.Model):
todolist = models.ForeignKey(ToDo, on_delete = models.CASCADE)
text = models.CharField(max_length = 300)
complete = models.NullBooleanField()
def __str__(self):
return self.text
我的文本是在该代码中定义的,但我不明白为什么会出现此错误,我尝试了所有方法
In [35]: lt.item_set.create(text = "moon", complete = False)
Out[35]: ---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
700 type_pprinters=self.type_printers,
701 deferred_pprinters=self.deferred_printers)
--> 702 printer.pretty(obj)
703 printer.flush()
704 return stream.getvalue()
~\anaconda3\lib\site-packages\IPython\lib\pretty.py in pretty(self, obj)
392 if cls is not object \
393 and callable(cls.__dict__.get('__repr__')):
--> 394 return _repr_pprint(obj, self, cycle)
395
396 return _default_pprint(obj, self, cycle)
~\anaconda3\lib\site-packages\IPython\lib\pretty.py in _repr_pprint(obj, p, cycle)
698 """A pprint that just redirects to the normal repr function."""
699 # Find newlines and replace them with p.break_()
--> 700 output = repr(obj)
701 lines = output.splitlines()
702 with p.group():
~\anaconda3\lib\site-packages\django\db\models\base.py in __repr__(self)
519
520 def __repr__(self):
--> 521 return '<%s: %s>' % (self.__class__.__name__, self)
522
523 def __str__(self):
~\Desktop\Django\Roman\Apple\funsite\apple\models.py in __str__(self)
16
17
---> 18 def __str__(self):
19 try:
20 return self.text
NameError: name 'text' is not defined
In [36]: lt.item_set.create(text = "moon", complete = False)
Out[36]: ---------------------------------------------------------------------------
NameError Traceback (most recent call last)
~\anaconda3\lib\site-packages\IPython\core\formatters.py in __call__(self, obj)
700 type_pprinters=self.type_printers,
701 deferred_pprinters=self.deferred_printers)
--> 702 printer.pretty(obj)
703 printer.flush()
704 return stream.getvalue()
~\anaconda3\lib\site-packages\IPython\lib\pretty.py in pretty(self, obj)
392 if cls is not object \
393 and callable(cls.__dict__.get('__repr__')):
--> 394 return _repr_pprint(obj, self, cycle)
395
396 return _default_pprint(obj, self, cycle)
~\anaconda3\lib\site-packages\IPython\lib\pretty.py in _repr_pprint(obj, p, cycle)
698 """A pprint that just redirects to the normal repr function."""
699 # Find newlines and replace them with p.break_()
--> 700 output = repr(obj)
701 lines = output.splitlines()
702 with p.group():
~\anaconda3\lib\site-packages\django\db\models\base.py in __repr__(self)
519
520 def __repr__(self):
--> 521 return '<%s: %s>' % (self.__class__.__name__, self)
522
523 def __str__(self):
~\Desktop\Django\Roman\Apple\funsite\apple\models.py in __str__(self)
16
17
---> 18 def __str__(self):
19 return self.text
NameError: name 'text' is not defined
我不明白为什么会出现这个错误
【问题讨论】:
-
你能在普通的shell中运行它吗(没有Ipython)?考虑到
NameError发生在self.text上并说text未定义,NameError非常奇怪,即使由于某种原因self没有属性text它应该给出AttributeError. -
是的,正常的 shell 没有显示任何错误
-
您可能想在您的问题中添加一些相关标签,例如ipython 等。还有那些google-bigquery 和sublimetext2 在您的问题中没有意义。 ..
-
我是一个初学者,我正在通过向您和其他用户学习该平台如何使用它
-
为什么要删除ipython 标签?您似乎不了解标签的用途。它们帮助关注这些标签的人找到您的问题。因此,您编辑出
ipython并添加sql、sqlite和database没有意义,因为您的问题在于ipython(正如您所说的在普通shell 中工作)而不是sql或@ 987654339@ 或database(如果您的问题甚至与这些无关,这些标签的专家将如何帮助您?)
标签: python django ipython nameerror