iirc, you can use http://docs.python.org/lib/module-StringIO.html to have a file like object from a string, and use that to make your django File object ( http://www.djangoproject.com/documentation/files/#the-file-object ).
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 ^,^ On Thu, Aug 28, 2008 at 4:44 PM, Jiri Barton <[EMAIL PROTECTED]> wrote: > > What is the preferred way of storing generated content into file > models? > > class Chart(models.Model): > xml = models.FileField(upload_to='charts') > ... > > I would like compute the image on the fly, using some data in the > database. How should I store the generated data? How should I use File > storage for this task? > > I would like Django to take care of the file naming for me. I would > like to use a one-liner such as > > Chart.objects.create(xml=default_storage.save('data.xml', data)) > > where data is a string, but that gives me > > AttributeError: 'str' object has no attribute 'chunks' > > 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' > > Should I use the class File directly? How? For File.save() to work, I > would need an associated an object. Please advise. > > Thank you > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---