Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I upload pickled data to django FileField?

I would like to store large dataset generated in Python in a Django model. My idea was to pickle the data to a string and upload it to FileField of my model. My django model is:

#models.py
from django.db import models

class Data(models.Model):
    label = models.CharField(max_length=30)
    file = models.FileField(upload_to="data")

In my Python program I would like to do the following:

import random, pickle

data_entry = Data(label="somedata")
somedata = [random.random() for i in range(10000)]
# Next line does NOT work 
#data_entry.file.save(filename, pickle.dumps(somedata))

How should I modify the last line to store somedata in file preserving the paths defined with upload_to parameter?

like image 202
btel Avatar asked May 11 '09 12:05

btel


2 Answers

Based on the answers to the questions I came up with the following solution:

from django.core.files.base import ContentFile
import pickle

content = pickle.dumps(somedata)
fid = ContentFile(content)
data_entry.file.save(filename, fid)
fid.close()

All of it is done on the server side and and users are NOT allowed to upload pickles. I tested it and it works all fine, but I am open to any suggestions.

like image 55
btel Avatar answered Oct 07 '22 17:10

btel


In you database the file attribute is just a path to the file. So, since you are not doing an actual upload you need to store the file on the disk and then save the path in database.

f = open(filename, 'w')
pickle.dump(somedata, f)
f.close()
data_entry.file=filename
data_entry.save()
like image 24
Sergey Golovchenko Avatar answered Oct 07 '22 17:10

Sergey Golovchenko