Fixed #16537 -- Fixed multi-db issues with GeoDjango utilities. Thanks, Shane Shifflett for the bug report and aaugustin for the initial patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16779 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2011-09-10 17:19:05 +00:00
parent 67dde2f52f
commit 36120f41a9
2 changed files with 9 additions and 9 deletions

View File

@ -132,9 +132,6 @@ class LayerMapping(object):
else: else:
raise LayerMapError('Unrecognized transaction mode: %s' % transaction_mode) raise LayerMapError('Unrecognized transaction mode: %s' % transaction_mode)
if using is None:
pass
#### Checking routines used during initialization #### #### Checking routines used during initialization ####
def check_fid_range(self, fid_range): def check_fid_range(self, fid_range):
"This checks the `fid_range` keyword." "This checks the `fid_range` keyword."
@ -393,7 +390,7 @@ class LayerMapping(object):
# Attempting to retrieve and return the related model. # Attempting to retrieve and return the related model.
try: try:
return rel_model.objects.get(**fk_kwargs) return rel_model.objects.using(self.using).get(**fk_kwargs)
except ObjectDoesNotExist: except ObjectDoesNotExist:
raise MissingForeignKey('No ForeignKey %s model found with keyword arguments: %s' % (rel_model.__name__, fk_kwargs)) raise MissingForeignKey('No ForeignKey %s model found with keyword arguments: %s' % (rel_model.__name__, fk_kwargs))
@ -429,7 +426,7 @@ class LayerMapping(object):
SpatialRefSys = self.spatial_backend.spatial_ref_sys() SpatialRefSys = self.spatial_backend.spatial_ref_sys()
try: try:
# Getting the target spatial reference system # Getting the target spatial reference system
target_srs = SpatialRefSys.objects.get(srid=self.geo_field.srid).srs target_srs = SpatialRefSys.objects.using(self.using).get(srid=self.geo_field.srid).srs
# Creating the CoordTransform object # Creating the CoordTransform object
return CoordTransform(self.source_srs, target_srs) return CoordTransform(self.source_srs, target_srs)

View File

@ -1,8 +1,7 @@
from django.contrib.gis.gdal import SpatialReference from django.contrib.gis.gdal import SpatialReference
from django.db import connections, DEFAULT_DB_ALIAS
def add_srs_entry(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None, def add_srs_entry(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None,
database=DEFAULT_DB_ALIAS): database=None):
""" """
This function takes a GDAL SpatialReference system and adds its information This function takes a GDAL SpatialReference system and adds its information
to the `spatial_ref_sys` table of the spatial backend. Doing this enables to the `spatial_ref_sys` table of the spatial backend. Doing this enables
@ -33,7 +32,11 @@ def add_srs_entry(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None,
of `django.db.DEFAULT_DB_ALIAS` (at the time of this writing, it's value of `django.db.DEFAULT_DB_ALIAS` (at the time of this writing, it's value
is 'default'). is 'default').
""" """
from django.db import connections, DEFAULT_DB_ALIAS
if not database:
database = DEFAULT_DB_ALIAS
connection = connections[database] connection = connections[database]
if not hasattr(connection.ops, 'spatial_version'): if not hasattr(connection.ops, 'spatial_version'):
raise Exception('The `add_srs_entry` utility only works ' raise Exception('The `add_srs_entry` utility only works '
'with spatial backends.') 'with spatial backends.')
@ -69,9 +72,9 @@ def add_srs_entry(srs, auth_name='EPSG', auth_srid=None, ref_sys_name=None,
try: try:
# Try getting via SRID only, because using all kwargs may # Try getting via SRID only, because using all kwargs may
# differ from exact wkt/proj in database. # differ from exact wkt/proj in database.
sr = SpatialRefSys.objects.get(srid=srs.srid) sr = SpatialRefSys.objects.using(database).get(srid=srs.srid)
except SpatialRefSys.DoesNotExist: except SpatialRefSys.DoesNotExist:
sr = SpatialRefSys.objects.create(**kwargs) sr = SpatialRefSys.objects.using(database).create(**kwargs)
# Alias is for backwards-compatibility purposes. # Alias is for backwards-compatibility purposes.
add_postgis_srs = add_srs_entry add_postgis_srs = add_srs_entry