我正在尝试覆盖模型实例__nonzero__
,因此它评估为False
我首先在一个简单的 Python 类上进行了测试:
>>> class A():
... pass
...
>>> a = A()
>>> a.__nonzero__ = lambda: False
>>> b = A()
>>> bool(a)
False
>>> bool(b)
True
>>> type(a).__nonzero__(a)
False
>>> type(b).__nonzero__(b)
True
这表明__nonzero__
被覆盖成功地改变了对象的bool()
评估。
然后我尝试将相同的东西应用于 django 模型对象(feincms.module.page.Page
它本身的实例继承自`django.db.models.Manager)。
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from feincms.module.page.modeladmins import PageAdmin
from feincms.module.page.models import Page
from guardian.admin import GuardedModelAdmin
from reversion import VersionAdmin
class PageAdmin(PageAdmin, GuardedModelAdmin, VersionAdmin):
# some stuff
# ...
#
def render_revision_form(self, *args, **kwargs):
"""Override render_revision_form so we can make ``obj`` evaluate to False
so ``feincms.module.page.forms.PageAdminForm.clean()`` does not try
to query ``self.page_manager.get(pk=current_id)`` where current_id
is ``obj.pk`` in the below function.
This is needed because ``self.page_manager.get(pk=current_id)`` is
queried on feincms Page objects rather than reversion Version objects.
"""
request, obj, version, context = args
obj.__nonzero__ = lambda: False
obj.hello = 'a'
import pdb
pdb.set_trace()
return super(PageAdmin, self).render_revision_form(request, obj, version, context, **kwargs)
admin.site.register(Page, PageAdmin)
但这评估为True
:
(Pdb) obj
<Page: test>
(Pdb) bool(obj)
True
有什么我不知道的原因可能导致这种行为吗?