views:

533

answers:

5

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?

A: 

Might you not be better off storing your data in a text field? It's not a file upload, after all.

Dominic Rodger
The data can be quite large so I would prefer not to store it in the database.
btel
+1  A: 

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()
Thanks for the answer! The problem is that I would like still to use FileField to generate paths and create directories.
btel
A: 

I've never done this, but based on reading a bit of the relevant code, I'd start by looking into creating an instance of django.core.files.base.ContentFile and assigning that as the value of the field.

Carl Meyer
+2  A: 

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.

btel
this is what i would have recommended
Paul Tarjan
+1  A: 

Marty Alchin has a section on this in Pro Django, which is (conveniently) available to read free on Google Books :-)

Dave Everitt