take a gdal spatialreference system and add its information to the spatialrefsys table of the spatial backend doing this enables databaselevel spatial transformations for the backend thus this utility is useful for adding spatial reference systems not included by default with the backend from django contrib gis utils import addsrsentry addsrsentry3857 keyword arguments authname this keyword may be customized with the value of the authname field defaults to epsg authsrid this keyword may be customized with the value of the authsrid field defaults to the srid determined by gdal refsysname for spatialite users only sets the value of the refsysname field defaults to the name determined by gdal database the name of the database connection to use the default is the value of django db defaultdbalias at the time of this writing its value is default if argument is not a spatialreference instance use it as parameter to construct a spatialreference instance initializing the keyword arguments dictionary for both postgis and spatialite backendspecific fields for the spatialrefsys model spatialite specific creating the spatialrefsys model try getting via srid only because using all kwargs may differ from exact wktproj in database take a gdal spatialreference system and add its information to the spatial_ref_sys table of the spatial backend doing this enables database level spatial transformations for the backend thus this utility is useful for adding spatial reference systems not included by default with the backend from django contrib gis utils import add_srs_entry add_srs_entry 3857 keyword arguments auth_name this keyword may be customized with the value of the auth_name field defaults to epsg auth_srid this keyword may be customized with the value of the auth_srid field defaults to the srid determined by gdal ref_sys_name for spatialite users only sets the value of the ref_sys_name field defaults to the name determined by gdal database the name of the database connection to use the default is the value of django db default_db_alias at the time of this writing its value is default if argument is not a spatialreference instance use it as parameter to construct a spatialreference instance initializing the keyword arguments dictionary for both postgis and spatialite backend specific fields for the spatialrefsys model spatialite specific creating the spatial_ref_sys model try getting via srid only because using all kwargs may differ from exact wkt proj in database
from django.contrib.gis.gdal import SpatialReference
from django.db import DEFAULT_DB_ALIAS, connections


def add_srs_entry(
    srs, auth_name="EPSG", auth_srid=None, ref_sys_name=None, database=None
):
    
    database = database or DEFAULT_DB_ALIAS
    connection = connections[database]

    if not hasattr(connection.ops, "spatial_version"):
        raise Exception("The `add_srs_entry` utility only works with spatial backends.")
    if not connection.features.supports_add_srs_entry:
        raise Exception("This utility does not support your database backend.")
    SpatialRefSys = connection.ops.spatial_ref_sys()

    
    
    if not isinstance(srs, SpatialReference):
        srs = SpatialReference(srs)

    if srs.srid is None:
        raise Exception(
            "Spatial reference requires an SRID to be "
            "compatible with the spatial backend."
        )

    
    
    kwargs = {
        "srid": srs.srid,
        "auth_name": auth_name,
        "auth_srid": auth_srid or srs.srid,
        "proj4text": srs.proj4,
    }
    
    srs_field_names = {f.name for f in SpatialRefSys._meta.get_fields()}
    if "srtext" in srs_field_names:
        kwargs["srtext"] = srs.wkt
    if "ref_sys_name" in srs_field_names:
        
        kwargs["ref_sys_name"] = ref_sys_name or srs.name

    
    try:
        
        
        SpatialRefSys.objects.using(database).get(srid=srs.srid)
    except SpatialRefSys.DoesNotExist:
        SpatialRefSys.objects.using(database).create(**kwargs)