Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将元素添加到数据库时发生NameError_Python_Django_Ipython_Nameerror - Fatal编程技术网

Python 将元素添加到数据库时发生NameError

Python 将元素添加到数据库时发生NameError,python,django,ipython,nameerror,Python,Django,Ipython,Nameerror,我一次又一次地犯这个错误,我不知道为什么我什么都试了,但似乎都不管用, 我正在尝试将元素添加到我的主页中 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)

我一次又一次地犯这个错误,我不知道为什么我什么都试了,但似乎都不管用, 我正在尝试将元素添加到我的主页中

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
[35]中的
:lt.item\u set.create(text=“moon”,complete=False)
出[35]:---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
调用中的~\anaconda3\lib\site packages\IPython\core\formatters.py(self,obj)
700 type\u PPRINTS=self.type\u打印机,
701延迟的打印机=自延迟的打印机)
-->702打印机。漂亮(obj)
703打印机。刷新()
704返回流。getvalue()
漂亮(self,obj)中的~\anaconda3\lib\site packages\IPython\lib\pretty.py
392如果cls不是对象\
393和可调用(cls.dict.get(“报告”):
-->394返回报告(obj、自我、循环)
395
396返回默认值(对象、自身、循环)
~\anaconda3\lib\site packages\IPython\lib\pretty.py in\u repr\u pprint(obj,p,cycle)
698“”“一个仅重定向到正常repr函数的pprint。”“”
699#找到换行符并替换为p.break())
-->700输出=repr(obj)
701行=输出。拆分行()
702带p.组():
~\anaconda3\lib\site packages\django\db\models\base.py in\uuuuu repr\uuuu(self)
519
520定义报告(自我):
-->521返回“”%(self.\u类\u.\u名称\u,self)
522
523 def_uuustr_uuu(自我):
~\Desktop\Django\Roman\Apple\funsite\Apple\models.py in\uuuu str\uuuu(self)
16
17
--->18定义(自我):
19尝试:
20返回self.text
名称错误:未定义名称“文本”
在[36]中:lt.item\u set.create(text=“moon”,complete=False)
出[36]:---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
调用中的~\anaconda3\lib\site packages\IPython\core\formatters.py(self,obj)
700 type\u PPRINTS=self.type\u打印机,
701延迟的打印机=自延迟的打印机)
-->702打印机。漂亮(obj)
703打印机。刷新()
704返回流。getvalue()
漂亮(self,obj)中的~\anaconda3\lib\site packages\IPython\lib\pretty.py
392如果cls不是对象\
393和可调用(cls.dict.get(“报告”):
-->394返回报告(obj、自我、循环)
395
396返回默认值(对象、自身、循环)
~\anaconda3\lib\site packages\IPython\lib\pretty.py in\u repr\u pprint(obj,p,cycle)
698“”“一个仅重定向到正常repr函数的pprint。”“”
699#找到换行符并替换为p.break())
-->700输出=repr(obj)
701行=输出。拆分行()
702带p.组():
~\anaconda3\lib\site packages\django\db\models\base.py in\uuuuu repr\uuuu(self)
519
520定义报告(自我):
-->521返回“”%(self.\u类\u.\u名称\u,self)
522
523 def_uuustr_uuu(自我):
~\Desktop\Django\Roman\Apple\funsite\Apple\models.py in\uuuu str\uuuu(self)
16
17
--->18定义(自我):
19返回self.text
名称错误:未定义名称“文本”

我无法正确理解为什么会出现此错误

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

您可以在普通shell中运行它(没有Ipython)吗?考虑到发生在
self.text
上的
namererror
非常奇怪,并且说
text
没有定义,即使出于某种原因,
self
没有属性
text
,它也应该给出一个
AttributeError
。是的,普通shell没有显示任何错误您可能想在问题中添加一些相关的标记,然后举例,还有这些,在你的问题中没有任何意义…我是一个初学者,我正在通过向你和其他用户学习来学习如何使用这个平台。你为什么要删除标签?你似乎不明白标签的用途。他们帮助那些跟随这些标签的人找到你的问题。因此,您编辑出
ipython
并添加
sql
sqlite
database
是没有意义的,因为您的问题是
ipython
(正如您所说,它在普通shell中工作)而不是
sql
sqlite
database
(如果您的问题与这些无关,这些标签的专家将如何帮助您呢?)我创建了文本字段,并在普通shell中运行这些行