Home > Mobile >  Change saved filepath in a django celery task
Change saved filepath in a django celery task

Time:05-05

I have a model named Flights

class Flights(models.Model):
    field = models.ForeignKey(Field, on_delete=models.CASCADE)
    datetime = models.DateTimeField(blank=True, null=True, default=timezone.now())

    nir = models.FileField(upload_to = user_directory_path_flights, null=True, blank=True)
    red = models.FileField(upload_to = user_directory_path_flights, null=True, blank=True)
    rededge = models.FileField(upload_to = user_directory_path_flights, null=True, blank=True)
    green = models.FileField(upload_to = user_directory_path_flights, null=True, blank=True)
   

User uploads some files and through a celery task i get those files and edit them into new ones. After that though they are saved at src folder when i want to save them at src/media/flights/username How do i do that ? Should i change the Flights model and add a filepath or something? And how so?

celery task :

import sys
import math
from .models import *
import cv2 as cv
import numpy as np
from PIL import Image, ImageOps, ImageChops
import PIL

@shared_task(bind=True)
def get_Flights_Data(self,flight_id):
    
    identifier = Flights.objects.get(pk=flight_id)
    redF = identifier.red
    nirF =  identifier.nir
    rededgeF = identifier.rededge
    print('Analyzing Flight')
    red = Image.open(redF)
    nir = Image.open(nirF)
    rededge = Image.open(rededgeF)
 ...............
 pil_image=Image.fromarray(ndvi)
    img = pil_image

    img.save("ndvi_agr.tiff", format="TIFF", save_all=True) #1

CodePudding user response:

Sadly you didn't ask your question correctly so I will try to help you as far as I can.

you can rewrite the previous file saved on the model. so all you have to do is saving it on the same file path after opening and changing the file.

red= Image.open(redF)
red.save(fp=redF.filepath)

The other option is you can delete the old one and save the new file and give the new file path to your model. and don't forget to save it.

  • Related