There's probably a better way than this though so you might want to
wait for other replies.
import StringIO
from django.core.files import File
f = StringIO.StringIO()
f.write(data)
myfile = File(f)
Chart.objects.create(xml=default_storage.save('data.xml', myfile))
Tim ^,^
You're nearly there for getting this to work, it's just the matter of
how you're passing in the data. Try importing ContentFile from
django.core.files.base, and using it like so:
Chart.objects.create(xml=default_storage.save('data.xml', ContentFile(data)))
> The example from http://www.djangoproject.com/documentation/files/#storage-objects
> does not work for me either:
>
>>>> from django.core.files.storage import default_storage
>
>>>> path = default_storage.save('/path/to/file', 'new content')
>
> This will give me
>
> <type 'exceptions.AttributeError'>: 'str' object has no attribute
> 'chunks'
Egad! Would you mind opening a ticket for this? That example is
definitely wrong, and will need to be updated.
-Gul
ha, I was almost certain that django wouldn't make it as hard as I had
explained it. :-D Glad to see it's this easy!
Tim ^,^
This should fix the error you got:
import StringIO
from django.core.files import File
f = StringIO.StringIO()
f.name, f.mode = 'data.xml', 'r'
f.write(data)
myfile = File(f)
Chart.objects.create(xml=default_storage.save(f.name, myfile))
However, Marty's solution seems a lot nicer. Did that not work? I've
never done anything like what I've shown you - it was more or less a
straight guess as to how it might work. Marty's solution looks a lot
more correct and a lot cleaner too. Perhaps you should be trying to
make that work instead?
Tim ^,^