Django queryset.first bug?可能涉及django-polymorphic和mixins

时间:2015-10-05 18:26:41

标签: python django mixins

我有一个模型,我已经实例化了几个实例并将它们保存到数据库中。通过python shell我可以确认对象存在,但是一旦我尝试通过queryset.first()调用处理查询集,我就会遇到麻烦,以及索引到queryset.all()结果。我正在使用django-polymorphic插件。

>>> Thing.objects.all()
[<Thing: thing1>, <Thing: thing2>, <Thing: thing3>]
>>> Thing.objects.first()
>>> str(Thing.objects.first())
'None'
>>> Thing.objects.all()[0]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File ".../python3.4/site-packages/django/db/models/query.py", line 201, in __getitem__
    return list(qs)[0]
IndexError: list index out of range
>>> Thing.objects.all().__class__
<class 'polymorphic.query.PolymorphicQuerySet'>
>>> 

我强烈怀疑这涉及到我在多态基类之上使用mixin for Thing的事实。这是我的层次结构的样子(简化为了让你们更容易):

from django.db import models
from polymorphic.polymorphic_model import PolymorphicModel

class AwesomeMixin(object):
    def reallyUsefulMethod(self):
        print('show me your MOVES')

class BaseThing(PolymorphicModel, AwesomeMixin):
    pass

class Thing(BaseThing):
    pass

class Doohickey(BaseThing):
    pass

class SomethingElse(models.Model, AwesomeMixin):
    pass

任何人都知道这里发生了什么?我正确使用mixins吗?问题与django-polymorphic有关吗?谢谢!

0 个答案:

没有答案