Re: Field pre-load hook?

2013-01-14 Thread Tom Evans
On Sun, Jan 13, 2013 at 8:00 PM, Matt Barry  wrote:
> Hi,
>
> I'm creating a model from an external database that has some encrypted
> columns.  Initially I was thinking of creating a custom field type:
>
> class EncryptedField(models.Field):
>   def db_type(self, connection):
> return 'TEXT'
>   def pre_save(self, model, add):
> return encrypt(getattr(model, self.attname))
>
> ..but it doesn't seem there are any hooks to decrypt the data on the loading
> side.  Am I missing anything, or is there a better way to do this sort of
> thing?
>
> Thanks,
> Matt
>

Hi Matt

get_db_prep_value() is called prior to saving in the database, this is
where you should encrypt the data.
to_python() is called on values loaded from the database, or
deserialised, this is where you should decrypt the data.

https://docs.djangoproject.com/en/dev/howto/custom-model-fields/#converting-database-values-to-python-objects

Cheers

Tom

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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.



Re: Field pre-load hook?

2013-01-14 Thread Derek
Maybe have a look at:

* http://www.tylerlesmann.com/2008/dec/19/encrypting-database-data-django/
* https://github.com/svetlyak40wt/django-fields#readme

On Sunday, 13 January 2013 22:00:24 UTC+2, Matt Barry wrote:
>
> Hi,
>
> I'm creating a model from an external database that has some encrypted 
> columns.  Initially I was thinking of creating a custom field type:
>
> class EncryptedField(models.Field):
>   def db_type(self, connection): 
> return 'TEXT'
>   def pre_save(self, model, add):
> return encrypt(getattr(model, self.attname))
>
> ..but it doesn't seem there are any hooks to decrypt the data on the 
> loading side.  Am I missing anything, or is there a better way to do this 
> sort of thing?
>
> Thanks,
> Matt
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/1okXPZcxQH8J.
To post to this group, send email to django-users@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.



Field pre-load hook?

2013-01-13 Thread Matt Barry
Hi,

I'm creating a model from an external database that has some encrypted 
columns.  Initially I was thinking of creating a custom field type:

class EncryptedField(models.Field):
  def db_type(self, connection): 
return 'TEXT'
  def pre_save(self, model, add):
return encrypt(getattr(model, self.attname))

..but it doesn't seem there are any hooks to decrypt the data on the 
loading side.  Am I missing anything, or is there a better way to do this 
sort of thing?

Thanks,
Matt

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/LYnZIyIzbfYJ.
To post to this group, send email to django-users@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.