How to detect clear checkbox chequed on ClearableFileInput

230 views
Skip to first unread message

Héctor Urbina

unread,
Feb 27, 2014, 10:46:02 AM2/27/14
to django...@googlegroups.com
Hello,

I have a edit view to modify an object. The model includes an inline formset of insumoRecurso objects, for including files associated with the main object. These files appear with a checkbox for clearing files (ClearableFileInput widget). What should I test on my view to detect that the checkbox has been selected?

Here's the code of my view:

@login_required
def editarRecurso(request, recurso_id):
  recurso = get_object_or_404(Recurso, pk=recurso_id)
  objetos = {}
  objetos["recurso"] = recurso
  u = request.user
  if recurso.creador != u and not (usuarioEsSupervisor(u) or usuarioEsCoordinador(u)):
      objetos["mensaje_de_error"] = "Usted no puede editar este recurso."
  else:
    InsumoFormset = inlineformset_factory(Recurso, InsumoRecurso, extra=1)
    if request.method == 'POST':
      recursoForm = RecursoForm(request.POST, instance=recurso)
      insumoFormset = InsumoFormset(request.POST, request.FILES, instance = recurso)
      if recursoForm.is_valid():
        recurso = recursoForm.save(commit=False)
        recurso.save()
        notificarCreacionRecurso(
request, recurso)
        for insumoForm in insumoFormset:
          if insumoForm.is_valid():
            insumoRecurso = insumoForm.save(commit=False)
            if not insumoRecurso.archivo.name: #This is the way I found for not saving objects that doesn't have an uploaded file (i.e., when user doesn't choose a file to upload)!!! is there a better way?
              continue
            ir = insumoRecurso.save()
            if insumoRecurso.archivo.remove: #This is not working, "remove" attribute doesn't exist... but here is where I expect a way of detecting clear checkbox selection. If there is no file, then the whole insumoRecurso object must be deleted.
              ir.delete()
        return redirect(reverse('recursos:detalle', args=(recurso.id,)))
      else:
        objetos["recursoForm"] = recursoForm
        objetos["insumoFormset"] = insumoFormset
    else:
      objetos["recursoForm"] = RecursoForm(instance=recurso)
      objetos["recursoForm"].fields["curso"].queryset = Curso.objects.filter(owner=request.user)
      objetos["insumoFormset"] = InsumoFormset(instance=recurso)

  template = loader.get_template('recursos/editarRecurso.html')
  context = RequestContext(request, objetos)
  return HttpResponse(template.render(context))


I would appreciate any help, By the way, even though I check the clear checkbox, the file is never deleted (I would assume that would happen on insumoRecurso.save(), but the files always remain there)

Thanks,
Hector.

Héctor Urbina

unread,
Mar 1, 2014, 10:43:32 AM3/1/14
to django...@googlegroups.com
Hello,

I ended with this code:

if insumoForm.is_valid() and not insumoForm.cleaned_data["DELETE"]:

            insumoRecurso = insumoForm.save(commit=False)
            if not insumoRecurso.archivo.name:
              continue
            insumoRecurso.recurso = recurso
            insumoRecurso.save()
Reply all
Reply to author
Forward
0 new messages