I'm trying to add a bunch of files from disk into my django database. Here's the helper function I've written so far:
def django_file(path, field_name, content_type): # adapted from here: http://groups.google.com/group/django-users/browse_thread/thread/834f988876ff3c45/ from django.core.files.uploadedfile import InMemoryUploadedFile f = open(path) return InMemoryUploadedFile( file=f, field_name=field_name, name=file.name, content_type=content_type, size=os.path.getsize(path), charset=None) I'm calling it like so: django_file("path-to-jpg-file", field_name="image", content_type="image/jpeg") Here's the error I'm getting: Traceback (most recent call last): File "<console>", line 1, in <module> File "/home/amarder/Documents/nmis/odk_dropbox/views.py", line 49, in import_instances_folder f = django_file(xml_files[0], field_name="xml_file", content_type="text/xml") File "/home/amarder/Documents/nmis/odk_dropbox/views.py", line 70, in django_file charset=None) File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- packages/django/core/files/uploadedfile.py", line 90, in __init__ super(InMemoryUploadedFile, self).__init__(file, name, content_type, size, charset) File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- packages/django/core/files/uploadedfile.py", line 30, in __init__ super(UploadedFile, self).__init__(file, name) File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- packages/django/core/files/base.py", line 17, in __init__ self.name = name File "/home/amarder/Documents/environments/nmis/lib/python2.6/site- packages/django/core/files/uploadedfile.py", line 46, in _set_name name = os.path.basename(name) File "/home/amarder/Documents/environments/nmis/lib/python2.6/ posixpath.py", line 111, in basename i = p.rfind('/') + 1 AttributeError: 'member_descriptor' object has no attribute 'rfind' Any suggestions? Andrew On Nov 16, 4:36 pm, Mitch Anderson <mi...@metauser.net> wrote: > On Tue, Nov 16, 2010 at 3:28 AM, Tom Evans <tevans...@googlemail.com> wrote: > > Django doesn't want a python file or text for a django file field, it > > wants a django.core.files.File. I find the easiest one to use is the > > InMemoryUploadedFile. Here is a snippet I use for fetching an image > > from the web, and creating a django.core.files.File object that can be > > assigned to a FileField or ImageField on a model: > > > h = httplib2.Http() > > req, content = h.request(uri) > > if req['status'] != '200': > > print u'Failed to fetch image from %s' % uri > > return None > > > import cStringIO > > from django.core.files.uploadedfile import InMemoryUploadedFile > > out = cStringIO.StringIO() > > out.write(content) > > return InMemoryUploadedFile( > > file=out, > > field_name=field, > > name=name, > > content_type=req['content-type'], > > size=out.tell(), > > charset=None) > > > field should be the name of the field on the model, name should be the > > file name of the resource. > > > There may be neater ways of doing this, but this keeps it in memory > > until django saves it to the upload_to location specified on the > > model, and avoids writing it to disk only for django to write it to > > disk again. > > > Cheers > > > Tom > > Awesome that worked perfectly! Thanks Tom! -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-us...@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.