In the interest of anyone following this issue, I must confess that my
previous solution broke down the minute I deployed it.  What was
interesting was that the `painful thing` - finding  the context from
which the __set__ is called, proved an impossible task without a
framework hook.

IMHO this specific problem cannot be solved using proxy descriptors as
I did in the solution above.

So I was back at 1^2 - finding a post_db_load-hook where I can change
a field's value immediately after loading from the db.  Since there is
none yet, I rolled my own and the QuerySet's iterator method seemed
the perfect spot for the hook.  I added this right at the end - just
before the final "yield obj" statement in the iterator method (i.e.
django.db.models.query.QuerySet.iterator):

------------------------------------------
for field in obj._meta.fields:
    if hasattr(field, 'get_db_post_load'):
        setattr(obj, field.attname,
field.get_db_post_load(getattr(obj, field.attname)))

yield obj
------------------------------------------

Then, in my CharEncryptField, I did away with the proxy descriptor
(quite reluctantly, actually - gotta luv that bit of slick:) and
hooked into my new custom hook:

class CharEncryptField(models.CharField):
    def encryptValue(self, value):
        return ''.join([chr(ord(char)+1) for char in str(value)])

    def decryptValue(self, value):
        return ''.join([chr(ord(char)-1) for char in str(value)])

    def get_internal_type(self):
        return "CharField"

    def get_db_prep_save(self, value):
        value = self.encryptValue(value)
        return super(CharEncryptField, self).get_db_prep_save(value)

    def get_db_post_load(self, value):
        value = self.decryptValue(value)
        # no super method here (yet)
        return value


This solved the problem with the context and looks a lot neater
`modulo` that boilerplate ;>


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to