Django 错误:AttributeError:“NoneType”对象没有属性“db”;

发布于 2024-11-30 17:05:59 字数 2182 浏览 1 评论 0原文

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

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

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'

Has anyone seen this error before? It comes whenever I try to execute a query on a particular model of mine. Querying the db directly works fine and it doesn't happen with other models.

For example, it's triggered by something like:

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

I believe this started immediately after a database migration with South. I could roll back the migration but I don't want to make bad worse so I've come here first.

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

Any ideas?

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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2024-12-07 17:05:59

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

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

新的教训:Django 模型上没有字段可以命名为“_state”。

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

Kicking myself for this one, especially given how obvious the error message makes it in retrospect.

My migration had added a new field named "_state" to the model. This field collided with the _state attribute of the object referenced in line 289 of query.pyc above.

So new lesson: no field can be named "_state" on a Django model.

Should this be submitted as a bug report?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文