[ZODB-Dev] Is any function called when an object is loaded from the database?

Claudiu Saftoiu csaftoiu at gmail.com
Tue Jun 19 21:38:39 UTC 2012


>
> You'll need to override ``__new__``. That's your hook. It's called
> when the database instantiates the object. Note that this is always
> true for Python. The ``__new__`` method is always called before an
> object is instantiated.
>

Actually, this doesn't seem to be what I want. ``__new__`` is called
*before* any attributes are set on the instance... so it's too early
to tell whether the instance is missing the attribute, as it certainly
will (since it's missing any attributes).

Is there any hook to call *after* the instance attributes get set/loaded
from the database?

Here is my code that didn't work, in case I'm just doing something silly:

class Line(Persistent):
    def __new__(cls, *args, **kwargs):
        inst = super(Line, cls).__new__(cls, *args, **kwargs)

        try:
            inst.id     #every instance in the DB already has an 'id'
        except AttributeError:
            return inst
        print 'we are here...' #this is never printed

        try:
            inst.expired  #the thing i actually want to guarantee
        except AttributeError:
            inst.expired = False

        return inst

Thanks,
- Claudiu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.zope.org/pipermail/zodb-dev/attachments/20120619/59d0b2e9/attachment.html>


More information about the ZODB-Dev mailing list