Django 错误:属性错误:'NoneType'对象没有属性'db'



有人见过这个错误吗?每当我试图对我的特定模型执行查询时,它就会出现。直接查询db工作得很好,它不会发生在其他模型上。

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

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'

为这一点自责,特别是考虑到回想起来错误信息是多么明显。

我的迁移已经向模型添加了一个名为"_state"的新字段。这个字段与查询的第289行中引用的对象的_state属性发生冲突。佩克以上。

所以新的教训:在Django模型中没有字段可以被命名为"_state"

应该作为bug报告提交吗?

相关内容

最新更新