From 8d5095d8a33824e02c95ee44d6e5acdb4b39e7ed Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Thu, 20 Jul 2017 22:26:22 +0500 Subject: [PATCH] Added BaseSpatialField.get_db_prep_value() to simplify. BaseSpatialField.get_db_prep_value() supersedes BaseSpatialField.get_db_prep_save() and Geometry.get_db_prep_value(). --- django/contrib/gis/db/models/fields.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/django/contrib/gis/db/models/fields.py b/django/contrib/gis/db/models/fields.py index a69c10b6b4..0e79033374 100644 --- a/django/contrib/gis/db/models/fields.py +++ b/django/contrib/gis/db/models/fields.py @@ -155,14 +155,13 @@ class BaseSpatialField(Field): else: return srid - def get_db_prep_save(self, value, connection): - """ - Prepare the value for saving in the database. - """ - if isinstance(value, Geometry) or value: - return connection.ops.Adapter(self.get_prep_value(value)) - else: + def get_db_prep_value(self, value, connection, *args, **kwargs): + if value is None: return None + return connection.ops.Adapter( + super().get_db_prep_value(value, connection, *args, **kwargs), + **({'geography': True} if self.geography and connection.ops.geography else {}) + ) def get_raster_prep_value(self, value, is_candidate): """ @@ -256,12 +255,6 @@ class GeometryField(GeoSelectFormatMixin, BaseSpatialField): kwargs['geography'] = self.geography return name, path, args, kwargs - def get_db_prep_value(self, value, connection, *args, **kwargs): - return connection.ops.Adapter( - super().get_db_prep_value(value, connection, *args, **kwargs), - **({'geography': True} if self.geography else {}) - ) - def from_db_value(self, value, expression, connection, context): if value: value = Geometry(value)