I'd like to use an upload handler function but confused as to how an uploaded file gets saved to a model's FileField. Eventually I will use a formset to allow for multiple files to be uploaded.
#models.py class MyModel(models.Model): file = models.FileField(upload_to='attachments', blank=True) comment = models.ForeignKey('Comment', blank=true, null=True) class Comment(models.Model): #forms.py class MyModelForm(forms.ModelForm): class Meta: model = MyModel #views.py def handle_uploads(key, folder=""): saved = {'original_title':key.name} upload_full_path = os.path.join(settings.MEDIA_ROOT, folder) if not os.path.exists(upload_full_path): os.makedirs(upload_full_path) counter = 1 while os.path.exists(os.path.join(upload_full_path, key.name)): name, ext = os.path.split(key.name) key.name = '%s_%s.%s' % (name, counter, ext) counter += 1 with open(os.path.join(upload_full_path, key.name), 'wb+') as destination: for chunk in key.chunks(): destination.write(chunk) destination.close() saved['file'] = destination return saved def add_comment(request, template_name='comments/comment_form.html'): """ I have two ModelForm's in my form. """ if request.POST: comment_form = ScopingCommentForm(request.POST, prefix='comment_form') attachment_form = MyModelForm(request.POST, request.FILES) if comment_form.is_valid() and attachment_form.is_valid(): comment = comment_form.save() if request.FILES.get('attachment'): f = handle_uploads(request.FILES.get('attachment'), settings.ATTACHMENTS) #this is where I feel like I am missing something 'obvious' try: logger.info("attachment path: %s"% f['original_title']) file = MyModel.objects.create(attachment=f['file'], comment=comment, original_title=f['original_title'], uploaded_on=datetime.datetime.now()) messages.add_message(request, messages.SUCCESS, """Your file has been successfully uploaded. The file %s has been renamed to %s"""% (orig_title, file.attachment.filename)) except DatabaseError, e: messages.error(request, "There was an error in uploading your file. Please rename and upload again. %s"% e) return render(request, template_name) else: comment_form = ScopingCommentForm(prefix='comment_form') attachment_form = ScopingAttachmentForm() return render(request, template_name, {'comment_form': comment_form, 'attachment_form':attachment_form}) #template <form action="." method="POST" id="comment-form" enctype="multipart/form-data"> {% csrf_token %} {{ comment_form.errors }} {{ comment_form.non_field_errors }} <fieldset class="one form-fieldset"> {{ comment_form.as_ul }} <div class="well"> {{ attachment_form.as_table }} </div> </fieldset> ... -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/xyNHxF07kJoJ. To post to this group, send email to django-users@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.