Basically, I need to use the User's password hash to encrypt some data via a custom model field. Check out the snippet I used here: Django Encryption.
I tried this:
class MyClass(models.Model): owner = models.ForeignKey(User) product_id = EncryptedCharField(max_length=255, user_field=owner) ................................................................................. def formfield(self, **kwargs): defaults = {'max_length': self.max_length, 'user_field': self.user_field} defaults.update(kwargs) return super(EncryptedCharField, self).formfield(**defaults))
But when I try to use user_field, I get a ForeignKey instance (of course!):
user_field = kwargs.get('user_field') cipher = user_field.password[:32]
Any help is appreciated!
maybe something like this - override the save() method where you can call encrypt method.
for decrypt you can use signal post_init, so every time you instantiate the model from the database the product_id field is decrypted automatically
maybe there is a more elegant "pythonic" way of doing this