1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import base64
from django.db import models
class Base64Field(models.TextField):
def contribute_to_class(self, cls, name):
if self.db_column is None:
self.db_column = name
self.field_name = name + '_base64'
super(Base64Field, self).contribute_to_class(cls, self.field_name)
setattr(cls, name, property(self.get_data, self.set_data))
def get_data(self, obj):
return base64.decodestring(getattr(obj, self.field_name))
def set_data(self, obj, data):
setattr(obj, self.field_name, base64.encodestring(data))
|
More like this
- base64 encoding/decoding property for storing binary data in Django TextFields. by bikeshedder 3 years, 11 months ago
- Automatic Manager Choice Filters by nipuL 4 years, 4 months ago
- SOAP views with on-demand WSDL generation by chewie 4 years, 10 months ago
- CompressedTextField by Digitalxero 3 years, 1 month ago
- Repeat blocks with new context / simple Jinja-like macro system by miracle2k 5 years, 10 months ago
Comments
To store values larger than 48k, add a db_type method that returns 'longtext'.
#
Great snippet, thank you. Very useful in my current situation (which is: don't don't don't want to store images in a Blob, but have been more or less commanded to, as a 'holdover' or whatever).
#
and great tip kylec, thank you too
#