I'm trying to override a model instance's __nonzero__
so it evaluates to False
I've first tested on a simple Python class:
>>> 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
This shows that __nonzero__
being overridden successfully alters the object's bool()
evaluation.
I then tried to apply the same thing to a django model object (instance of feincms.module.page.Page
that itself inherits from `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)
but this evaluates to True
:
(Pdb) obj
<Page: test>
(Pdb) bool(obj)
True
Is there any reason that am I not aware of that could cause this behaviour?
You are using old-style class in this example. And model instance is a new-style class.
class B(object): pass
b = B()
b.__nonzero__ = lambda: False
bool(b) # True
Apparently, you can't override special methods for the instances, only for types.
B.__nonzero__ = lambda self: False
bool(b) # False
Read the following SO question for examples and more detailed explanation: "overriding-special-methods-on-an-instance"