1

I have a function that process images that are uploaded by users. I made a view that when entered apply the function on the uploaded images.

models.py

class UploadedImages(models.Model):
    patient =           models.ForeignKey(Patient,on_delete=models.CASCADE,related_name='images')
    pre_analysed =      models.FileField(upload_to = user_directory_path ,
                                            verbose_name = 'Image')

class Processed(models.Model):
    uploaded_image = models.ForeignKey(UploadedImages,on_delete=models.CASCADE,related_name='processed')
    analysedimage = models.ImageField(upload_to=analyses_directory_path,
                                      verbose_name='analysed Image', blank=True)

views.py

def AnalysedDetails(request,pk=None):
    Uimage = get_object_or_404(models.UploadedImages,pk=pk)
    analysed = models.Processed(uploaded_image=Uimage,analysedimage=main(Uimage.pre_analysed.path))
    analysed.save()
    return HttpResponse("OK")

but when i check the admin page to see the model it only saves the uploaded image and doesn't save the processed one and when I check the directory i find that only the uploaded image field is saved.

I tried

analysed=models.Processed(uploaded_image=Uimage.pre_analysed,analysedimage=main(Uimage.pre_analysed.path))

but it returns

Cannot assign "": "Processed.uploaded_image" must be a "UploadedImages" instance

Ammar rady
  • 136
  • 2
  • 13

1 Answers1

1

to create new object, theres simple way, using .create(),
see this how to create object
so, it would be
analysed = models.Processed.objects.create(...)
and you don't need save() method if you use this way

to save ImageField programmatically, you can see this or this

lieahau
  • 498
  • 4
  • 9
  • I found out that the problem is with the function as it saves the file in the same path with a different name look at this question for more info and btw thanks for your help really appreciate it. – Ammar rady Aug 07 '19 at 19:18