【问题标题】:NameError Occured when I add element into databaseNameError 当我将元素添加到数据库时发生
【发布时间】: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-bigquerysublimetext2 在您的问题中没有意义。 ..
  • 我是一个初学者,我正在通过向您和其他用户学习该平台如何使用它
  • 为什么要删除ipython 标签?您似乎不了解标签的用途。它们帮助关注这些标签的人找到您的问题。因此,您编辑出ipython 并添加sqlsqlitedatabase 没有意义,因为您的问题在于ipython(正如您所说的在普通shell 中工作)而不是sql 或@ 987654339@ 或 database (如果您的问题甚至与这些无关,这些标签的专家将如何帮助您?)

标签: python django ipython nameerror


【解决方案1】:
  • 首先,在您的终端中,cd 到您的项目文件夹并激活 venv
  • 修改models.py中的代码后,运行pyhton manage.py makemigrations [如果您已经执行了命令,您将在 apps 迁移文件夹 中看到一个文件,例如 0001_initial.py]
  • 然后运行python manage.py migrate

如果您的问题仍然存在,请提供更多详细信息。

【讨论】:

    【解决方案2】:

    您是否进行过任何迁移并运行它们来创建字段text?如果没有,那么您需要使用 ./manage.py makemigrations ./manage.py migrate 运行它们。

    【讨论】:

    • 我创建了文本字段并在普通 shell 中运行该行
    猜你喜欢
    • 1970-01-01
    • 2021-01-18
    • 2012-05-31
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-06
    相关资源
    最近更新 更多