Django storage.path does not get referenced in the Model

13 Views Asked by At

I am trying to save a file in a Model's file field. I do this specifying the field storage path and writing to that path.

class Layer(models.Model):
    name = models.CharField(max_length=50, unique=True)
    file = models.FileField(upload_to='layers')

My use case is writing a GDAL dataset to the path:

instance = Layer.objects.get(pk=1)
name = 'layers/test.tif'
out = instance.file.storage.path(name=filename) # use this path

dataset = gdal.Open("/path/to/dataset.tif")
warp = gdal.Warp(out, dataset, dstSRS="EPSG:4326") # write to that path
instance.save()

The file gets written in the path. However the model does not reference to it:

print('out', instance.file.path)

Gets this error:

ValueError: The 'file' attribute has no file associated with it.

What Model FileField method or Save method should I call so I can reference it?

0

There are 0 best solutions below