0

I have this model

class TravelRequests(models.Model):
mission_order           = models.FileField(blank=True, null=True, upload_to='mission_order')

where the mission order filefield is not required for the creation of the model but than it will be updated by an another user so he can upload the file. The problem is that when i upload the file when the model is created it doesn't save the file in the /media folder and in the filefield it self it save only the name of the file

this is the ajax request :

            var data = new FormData($('.facilities_form').get(0));
            var pk = $(this).val()
            $.ajax({
           type: "POST",
           url: '/approve_facilities/' + pk + '/',
           data: data,
           cache: false,
           processData: false,
           contentType: false,
           success: function(response) {
            if (response == 'request_updated') {

            Swal.fire({
              title: 'The request is approved!',
              type: 'success',
              allowOutsideClick: false,
            }).then((result) => {
          if (result.value) {
            window.location.href='/'
          }
        });
            }
            }
            });

this is the view that handle the request

@login_required
def FacilitiesApproveRequest(request, pk):
    cordinator_comment = request.POST.get('cordinator_comment', '').strip()
    mission_order = request.FILES.get('mission_order')
    request_toApprove = TravelRequests.objects.filter(pk=pk)
    request_toApprove.update(mission_order=mission_order, facilities_approve_case=True,
        facilities_comment=cordinator_comment, facilities_comment_time=timezone.now(),
        request_updater=request.user, request_update=timezone.now(),
        request_status='facility_approve', facilities_aprove=request.user)
    return HttpResponse('request_updated')

this is the html input :

<form class="facilities_form" method="post" enctype="multipart/form-data">
<input type="file" name="mission_order" id="id_mission_order" accept=".pdf,.jpeg,.png,.msg">
</form>

when i use the form to update the field i get this enter image description here

Only the filename without the path and when i look into the mission order folder i don't find the uploaded file.

Any help will be appreciated.

Farhani Walid
  • 927
  • 1
  • 9
  • 20
  • Have you tried to assign `request_toApprove.mission_order = mission_order` and then call `.save()` explicitly? – Ralf Aug 23 '19 at 15:49
  • it works, thank you, do you mind to explain why it works with the .save() method and not .update() ?? – Farhani Walid Aug 23 '19 at 16:03

1 Answers1

0

Have you tried to assign request_toApprove.mission_order = mission_order and then call .save() explicitly?

Maybe like this:

def FacilitiesApproveRequest(request, pk):
    ...
    mission_order = request.FILES.get('mission_order')
    request_toApprove = TravelRequests.objects.filter(pk=pk)
    request_toApprove.mission_order = mission_order
    request_toApprove.facilities_approve_case = True
    ...
    request_toApprove.save()
    ...

The queryset.update() method does not call the .save() method of the model instances (read the last paragraph of this doc section), which is necessary for the underlying file to be saved correctly.

See this answer to a similar question and this question.

Ralf
  • 16,086
  • 4
  • 44
  • 68