That worked, thanks :)
On Dec 10, 7:51 pm, Rajesh Dhawan <[EMAIL PROTECTED]> wrote:
> On Dec 10, 10:47 am, Mikkel Høgh <[EMAIL PROTECTED]> wrote:
>
>
>
> > Ok, I've been looking all over the docs for a solution to this, but
> > there's a lot of documentation for the FileField, just not on how to
> > use it…
>
> > I have a model that looks like this:
>
> > class Import(models.Model):
> > complete_data = models.FileField(upload_to='import_data/complete/
> > %Y/%m/')
> > partial_data = models.FileField(upload_to='import_data/partial/%Y/
> > %m/')
> > user = models.ForeignKey(User, blank=False, null=False)
>
> > I would like to do something like this:
>
> > import_data = Import.objects.create(user=user_object)
> > file_name = hashlib.sha1(user_object.username + str(time.time
> > ())).hexdigest()
> > import_data.complete_data.name = file_name
> > import_data.complete_data.open('w')
> > import_data.complete_data.write(pickle.dumps(pickles))
> > import_data.complete_data.close()
>
> > But it seems these attributes are read-only… So, the golden question
> > is, how do I pick a file name and write to a FileField?
>
> In short, you need to use the save method on the FileField:
>
> import_data.complete_data.save(file_name, file_content)
>
> Where file_content is an instance of django.core.files.File.
>
> Specifically, it would work like this:
>
> from django.core.files import ContentFile
>
> import_data = Import.objects.create(user=user_object)
> file_name = hashlib.sha1(user_object.username + str(time.time
> ())).hexdigest()
> file_content = ContentFile(pickle.dumps(pickles))
> import_data.complete_data.save(file_name, file_content)
>
> The above ContentFile implementation of File is good for in-memory
> file content. So, if your pickle.dumps() call results in a small
> enough chunk of data, the above use of ContentFile would be good
> enough.
>
> If it can be a big chunk of data or you don't want the interim pickled
> content in memory for any reason, you can use an instance of
> django.core.files.uploadedfile.TemporaryUploadedFile instead.
> Alternatively, you can use the base django.core.files.File which
> serves as a wrapper around an open file.
>
> -Rajesh D
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---