【问题标题】:Django error: AttributeError: 'NoneType' object has no attribute 'db'Django 错误:AttributeError:“NoneType”对象没有属性“db”
【发布时间】:2011-08-21 22:37:16
【问题描述】:

以前有人见过这个错误吗?每当我尝试对我的特定模型执行查询时,它就会出现。直接查询数据库可以正常工作,其他模型不会发生。

例如,它由以下内容触发:

MyModel.objects.get(name__iexact = 'an existent name')

我相信这是在与 South 进行数据库迁移后立即开始的。我可以回滚迁移,但我不想让情况变得更糟,所以我先来这里。

  • Django 1.3
  • PostgreSQL 8.4.8
  • Python 2.7.0
  • iPython 0.10.2
  • Ubuntu 10.10 64 位

有什么想法吗?

ERROR: An unexpected error occurred while tokenizing input
The following traceback may be corrupted or invalid
The error message is: ('EOF in multi-line statement', (173, 0))

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)

/home/<path to python>/<ipython console> in <module>()

/home/<path to python>/python2.7/site-packages/django/db/models/manager.pyc in get(self, *args, **kwargs)
    130 
    131     def get(self, *args, **kwargs):
--> 132         return self.get_query_set().get(*args, **kwargs)
    133 
    134     def get_or_create(self, **kwargs):

/home/<path to python>/python2.7/site-packages/django/db/models/query.pyc in get(self, *args, **kwargs)
    342         if self.query.can_filter():
    343             clone = clone.order_by()
--> 344         num = len(clone)
    345         if num == 1:
    346             return clone._result_cache[0]

/home/<path to python>/python2.7/site-packages/django/db/models/query.pyc in __len__(self)
     80                 self._result_cache = list(self._iter)
     81             else:
---> 82                 self._result_cache = list(self.iterator())
     83         elif self._iter:
     84             self._result_cache.extend(self._iter)

/home/<path to python>/python2.7/site-packages/django/db/models/query.pyc in iterator(self)
    287 
    288                 # Store the source database of the object

--> 289                 obj._state.db = db
    290                 # This object came from the database; it's not being added.

    291                 obj._state.adding = False

AttributeError: 'NoneType' object has no attribute 'db'

【问题讨论】:

  • 如果您能够发布模型类,它会有所帮助。这可能就是问题所在。
  • 另外 - 值得发布 DATABASES 的设置值,因为这很重要:docs

标签: python django django-south


【解决方案1】:

为此而自责,尤其是考虑到错误消息在回想起来有多明显。

我的迁移在模型中添加了一个名为“_state”的新字段。该字段与上面 query.pyc 的第 289 行中引用的对象的 _state 属性发生冲突。

新课:在 Django 模型上,没有字段可以命名为“_state”。

这应该作为错误报告提交吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-20
    • 2018-02-11
    • 2016-10-21
    • 1970-01-01
    • 1970-01-01
    • 2013-11-21
    • 2017-03-10
    相关资源
    最近更新 更多