I want this field to be saved encrypted in the db, but appear
unencrypted everywhere else.    My question is which combination of
field methods should I use?

I have gotten parts of this working with various combinations of the
methods below, but can't get it all to work.   Using one method makes
modelform save work, but then I can't access the field
directly with model.secret because it is still encrypted.   If I use
the to_python method, I can
access the field with model.secret but the modelform handling breaks.

class EncryptedCharField(models.CharField):
    __metaclass__ = models.SubfieldBase

    def save_form_data(self, instance, data):
        setattr(instance, self.name, encrypt(data))

    def value_from_object(self, obj):
        return decrypt(getattr(obj, self.attname))

    #def to_python(self, value):
    #    return decrypt(value)

class MyModel(models.Model):
     secret = EncryptedCharField(max_length=100)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to