Ticket #10757: 10757_v2.diff
File 10757_v2.diff, 5.6 KB (added by , 16 years ago) |
---|
-
django/contrib/gis/db/models/sql/query.py
74 74 table = self.alias_map[alias][TABLE_NAME] 75 75 if table in only_load and col not in only_load[table]: 76 76 continue 77 r = self.get_field_select(field, alias )77 r = self.get_field_select(field, alias, column) 78 78 if with_aliases: 79 79 if col[1] in col_aliases: 80 80 c_alias = 'Col%d' % len(col_aliases) … … 112 112 113 113 # This loop customized for GeoQuery. 114 114 for (table, col), field in izip(self.related_select_cols, self.related_select_fields): 115 r = self.get_field_select(field, table )115 r = self.get_field_select(field, table, col) 116 116 if with_aliases and col in col_aliases: 117 117 c_alias = 'Col%d' % len(col_aliases) 118 118 result.append('%s AS %s' % (r, c_alias)) … … 213 213 values = [self.convert_values(v, self.extra_select_fields.get(a, None)) 214 214 for v, a in izip(row[rn_offset:index_start], aliases)] 215 215 if SpatialBackend.oracle or getattr(self, 'geo_values', False): 216 # We resolve the columns 216 # We resolve the columns 217 217 for value, field in izip(row[index_start:], fields): 218 218 values.append(self.convert_values(value, field)) 219 219 else: … … 260 260 sel_fmt = sel_fmt % self.custom_select[alias] 261 261 return sel_fmt 262 262 263 def get_field_select(self, f ld, alias=None):263 def get_field_select(self, field, alias=None, column=None): 264 264 """ 265 265 Returns the SELECT SQL string for the given field. Figures out 266 266 if any custom selection SQL is needed for the column The `alias` 267 267 keyword may be used to manually specify the database table where 268 268 the column exists, if not in the model associated with this 269 `GeoQuery`. 269 `GeoQuery`. Similarly, `column` may be used to specify the exact 270 column name, rather than using the `column` attribute on `field`. 270 271 """ 271 sel_fmt = self.get_select_format(f ld)272 if f ld in self.custom_select:273 field_sel = sel_fmt % self.custom_select[f ld]272 sel_fmt = self.get_select_format(field) 273 if field in self.custom_select: 274 field_sel = sel_fmt % self.custom_select[field] 274 275 else: 275 field_sel = sel_fmt % self._field_column(f ld, alias)276 field_sel = sel_fmt % self._field_column(field, alias, column) 276 277 return field_sel 277 278 278 279 def get_select_format(self, fld): … … 302 303 return sel_fmt 303 304 304 305 # Private API utilities, subject to change. 305 def _field_column(self, field, table_alias=None ):306 def _field_column(self, field, table_alias=None, column=None): 306 307 """ 307 308 Helper function that returns the database column for the given field. 308 309 The table and column are returned (quoted) in the proper format, e.g., 309 310 `"geoapp_city"."point"`. If `table_alias` is not specified, the 310 311 database table associated with the model of this `GeoQuery` will be 311 used. 312 used. If `column` is specified, it will be used instead of the value 313 in `field.column`. 312 314 """ 313 315 if table_alias is None: table_alias = self.model._meta.db_table 314 316 return "%s.%s" % (self.quote_name_unless_alias(table_alias), 315 self.connection.ops.quote_name( field.column))317 self.connection.ops.quote_name(column or field.column)) 316 318 317 319 def _geo_field(self, field_name=None): 318 320 """ -
django/contrib/gis/tests/relatedapp/tests.py
183 183 self.assertEqual(m.point, d['point']) 184 184 self.assertEqual(m.point, t[1]) 185 185 186 # Test disabled until #10572 is resolved.187 186 def test08_defer_only(self): 188 187 "Testing defer() and only() on Geographic models." 189 188 qs = Location.objects.all() … … 191 190 for loc, def_loc in zip(qs, def_qs): 192 191 self.assertEqual(loc.point, def_loc.point) 193 192 193 def test09_pk_relations(self): 194 "Ensuring correct primary key column is selected across relations. See #10757." 195 # Adding two more cities, but this time making sure that their location 196 # ID values do not match their City ID values. 197 loc1 = Location.objects.create(point='POINT (-95.363151 29.763374)') 198 loc2 = Location.objects.create(point='POINT (-96.801611 32.782057)') 199 dallas = City.objects.create(name='Dallas', location=loc2) 200 houston = City.objects.create(name='Houston', location=loc1) 201 202 # The expected ID values -- notice the last two location IDs 203 # are out of order. We want to make sure that the related 204 # location ID column is selected instead of ID column for 205 # the city. 206 city_ids = (1, 2, 3, 4, 5) 207 loc_ids = (1, 2, 3, 5, 4) 208 ids_qs = City.objects.order_by('id').values('id', 'location__id') 209 for val_dict, c_id, l_id in zip(ids_qs, city_ids, loc_ids): 210 self.assertEqual(val_dict['id'], c_id) 211 self.assertEqual(val_dict['location__id'], l_id) 212 194 213 # TODO: Related tests for KML, GML, and distance lookups. 195 214 196 215 def suite():