currently I’m trying to get multi upload in my admin interface working.
I’m using the code Luril Garmash provided in his blog:
http://garmoncheg.blogspot.de/2011/07/django-creating-multi-upload-form.html
He’s using the very nice jQuery plugin for multi uploading files from Sebastian Tschan:
http://blueimp.github.com/jQuery-File-Upload/
So do I.
Okay, here’s my structure:
models.py:
class Photoalbum(models.Model):
event = models.OneToOneField(Event, help_text="Liste der Events ohne Fotoalbum")
date = models.DateField(auto_now=True)
facebook = models.BooleanField()
class PhotoalbumImage(models.Model):
album = models.ForeignKey(Photoalbum, related_name='images', null=True)
filename = models.CharField(max_length=60, blank=True, null=True, editable=False)
image = models.FileField(upload_to=storage, editable=False)
key_data = models.CharField(max_length=90, unique=True, blank=True, null=True, editable=False)
upload_date = models.DateTimeField(auto_now=True)
I extended the change_form.html template for Photoalbum in order to show the jQuery multi upload form on the add / change site of the Photoalbum model.
Here’s the view logic:
views.py:
def multiuploader(request):
#getting file data for farther manipulations
file = request.FILES[u'files[]']
wrapped_file = UploadedFile(file)
filename = wrapped_file.name
file_size = wrapped_file.file.size
log.info ('Got file: "%s"' % str(filename))
#writing file manually into model
#because we don't need form of any type.
image = PhotoalbumImage()
image.filename=str(filename)
image.image=file
image.key_data = image.key_generate
image.save()
log.info('File saving done')
#getting thumbnail url using sorl-thumbnail
im = get_thumbnail(image, "80x80", quality=50)
thumb_url = im.url
#settings imports
try:
file_delete_url = settings.MULTI_FILE_DELETE_URL+'/'
file_url = settings.MULTI_IMAGE_URL+'/'+image.key_data+'/'
except AttributeError:
file_delete_url = 'multi_delete/'
file_url = 'multi_image/'+image.key_data+'/'
#generating json response array
result = []
result.append({"id":image.id,
"name":filename,
"size":file_size,
"url":file_url,
"thumbnail_url":thumb_url,
"delete_url":file_delete_url+str(image.pk)+'/',
"delete_type":"POST",})
response_data = json.dumps(result)
#checking for json data type
#big thanks to Guy Shapiro
if "application/json" in request.META['HTTP_ACCEPT_ENCODING']:
mimetype = 'application/json'
else:
mimetype = 'text/plain'
return HttpResponse(response_data, mimetype=mimetype)
else: #GET
return HttpResponse('Only POST accepted')
this method is called for each file uploaded by the plugin.
note: the file upload saves the files to the filesystem and database seperately from the photoalbum, by clicking on “start upload”.
Note: my photoalbum add / change template shows the multiuploader for my photoalbumimages on the top with “add files” and “start upload” and on the bottom the form fields for the photoalbum on the bottom with the standard django admin save buttons.
sry that i can’t provide a picture because my connection is very, very slow (TOO SLOW) at the place I’m right now.
Thats the point were my problem begins: the files are saved but the photoalbum isn’t. I need a save way to save the correct primary key of the photoalbum into each saved file as foreign key.
My first idea was to handle this problem with the session, save all saved file id’s into a list and within the post_save signal for the photoalbum, get those id’s and save the correct photoalbum id to those files. But I don’t think one should access the session within the model.
My second idea is to create a temp model, e.g. UploadedPhotos, where I store all the file id’s and on post_save signal for the photoalbum, get those id’s.
This just doesn’t feel right.
I think the safest way to accomplish my needs would be to upload the files AFTER the photoalbum has been created (after clicking on Save in the Photoalbum model). Then a primary key for the photoalbum would exist and I could save it to each file.
But I don’t have any idea to get this working.
Big thanks for your help in advance.
Greetings
okay i achieved my requirements trough populating a hidden form field with the uploaded and saved id’s of the photos in the photoalbum model. this field is a charfield and i put all the id’s in it and seperate them with “//” and parse it on the post_save signal of the photoalbum.