How to save a raster in RasterField after save in GeoDjango?

I am building an app using Django with PostGIS backend. Thus, GeoDjango. I have a model with FileField, and RasterField [1]. I want to save a raster object to RasterField after supplying the FileField. I am trying to achieve this using a post_save signal.

class Layer(models.Model):
    name = models.CharField(max_length=50, blank=True)
    file = models.FileField(
        upload_to='layers',
        null=True,
        max_length=500)
    raster_file = RasterField(
        null=True,
        blank=True,
    )

After some research [2], you have to use GDALRaster [3] to make the raster as an object. In my signals:

from django.contrib.gis.gdal import GDALRaster
from data_management.models import Layer


@receiver(post_save, sender=Layer)
def create_raster(sender, instance, **kwargs):
    instance.raster_file = GDALRaster(instance.file.path)
    instance.save()

When I am uploading the file in my admin, the raster_file is not filled and not created. How do I save the raster, which is uploaded through the file field, in my raster_file field?

enter image description here

Back to Top