I have been using pandas in python and I usually write a dataframe to my db table as below. I am now now migrating to Django, how can I write the same dataframe to a table t
My solution using pickle and optionally zlib for compression
import pickle
#optional
#import zlib
class SuperModel(models.Model):
DFToStore = models.BinaryField(default=None, null=True, blank=True)
def save(self, *args, **kwargs):
if not isinstance(self.DFToStore, (bytes)):
self.DFToStore = pickle.dumps(self.DFToStore)
#optional with compression
#self.DFToStore = zlib.compress(pickle.dumps(self.DFToStore))
super(SuperModel, self).save(*args, **kwargs)
def get_DFToStore(self):
if isinstance(self.DFToStore, (bytes)):
return pickle.loads(self.DFToStore)
#optional with compression
#return pickle.loads(zlib.decompress(self.DFToStore))
if not isinstance(self.DFToStore, (bytes)):
return self.DFToStore