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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2bbe12a5-c070-4b10-85d4-406c9053a8c3%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to