1
votes

Im a bit frustared trying to update a photo from a form. I have this:

<form action="/subir-fotos/{{campana_id}}/{{point_id}}/" method="POST" enctype="multipart/form-data" id="form_tomaFoto{{page}}">{% csrf_token %}

<input type="hidden" value="{{i.titulo_foto.id}}" name="titulo">
<p><input type="file" accept="image/*" name="foto" required></p>

<p><input type="submit" value="Enviar foto" class="boton"></p>
</div>
</form>

My model:

class InteraccionFotos(models.Model):
    campana = models.ForeignKey('dashboard.Campana')
    titulo_foto = models.ForeignKey('actividad_fotos.TitulosFotos')
    punto_interaccion = models.ForeignKey(PuntoInteraccion)
    foto = models.ImageField(upload_to='.',blank=True,null=True)

Then, in my View, I have this:

class SubeFotoView(FormView):
    form_class = FilebabyForm
    success_url = '/'
    template_name = 'tomar_fotos.html'

    def form_valid(self,form):
        form.save(commit=True)
        messages.success(self.request, 'File uploaded!')
        return super(SubeFotoView, self).form_valid(form)


    def post(self, request, *args, **kwargs):
        campana = self.kwargs.get('campana_id')
        point_id = self.kwargs.get('point_id')
        titulo_foto = request.POST['titulo']
        foto = request.FILES['foto']
        crea_foto = InteraccionFotos.objects.filter(pk=1,campana_id=3,punto_interaccion_id=5).update(foto=foto)
        data = {}
        data['success'] = "Foto subida con exitosamente"
        return HttpResponse(json.dumps(data), content_type='application/json')

As you can see in crea_foto variable I have a queryset where I made a filter and then update the field foto, but I can't get it works. Thing is if instead filter, I do a create queryset works fine and create the photo, in the admin if I upload from scratch or update a photo works fine too. Only happens with update queryset, any clue of how to solve it?

PS: I'm using Boto, but since images are uploaded with other methods it is discarded that Boto be the problem.

1

1 Answers

6
votes

It was really easy, just add this in POST method:

m = InteraccionFotos.objects.get(pk=1)
m.foto = request.FILES['foto']
m.save()