Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added field.attname to Options.name_map

The change also removed allow_explicit_fk from sql/query.py.
  • Loading branch information...
commit c21e86ab9e3e5ebd6d245d038cb0cb352cd84c3a 1 parent 31e6d58
Anssi Kääriäinen authored June 18, 2013
7  django/db/models/options.py
@@ -404,12 +404,13 @@ def init_name_map(self):
404 404
         for f, model in self.get_all_related_objects_with_model():
405 405
             cache[f.field.related_query_name()] = (f, model, False, False)
406 406
         for f, model in self.get_m2m_with_model():
407  
-            cache[f.name] = (f, model, True, True)
  407
+            cache[f.name] = cache[f.attname] = (f, model, True, True)
408 408
         for f, model in self.get_fields_with_model():
409  
-            cache[f.name] = (f, model, True, False)
  409
+            cache[f.name] = cache[f.attname] = (f, model, True, False)
410 410
         for f in self.virtual_fields:
411 411
             if hasattr(f, 'related'):
412  
-                cache[f.name] = (f.related, None if f.model == self.model else f.model, True, False)
  412
+                cache[f.name] = cache[f.attname] = (
  413
+                    f.related, None if f.model == self.model else f.model, True, False)
413 414
         if app_cache_ready():
414 415
             self._name_map = cache
415 416
         return cache
29  django/db/models/sql/query.py
@@ -1091,8 +1091,7 @@ def build_filter(self, filter_expr, branch_negated=False, current_negated=False,
1091 1091
 
1092 1092
         try:
1093 1093
             field, sources, opts, join_list, path = self.setup_joins(
1094  
-                    parts, opts, alias, can_reuse, allow_many,
1095  
-                    allow_explicit_fk=True)
  1094
+                    parts, opts, alias, can_reuse, allow_many,)
1096 1095
             if can_reuse is not None:
1097 1096
                 can_reuse.update(join_list)
1098 1097
         except MultiJoin as e:
@@ -1237,15 +1236,14 @@ def _add_q(self, q_object, used_aliases, branch_negated=False,
1237 1236
                                      len(q_object.children))
1238 1237
         return target_clause
1239 1238
 
1240  
-    def names_to_path(self, names, opts, allow_many, allow_explicit_fk):
  1239
+    def names_to_path(self, names, opts, allow_many):
1241 1240
         """
1242 1241
         Walks the names path and turns them PathInfo tuples. Note that a
1243 1242
         single name in 'names' can generate multiple PathInfos (m2m for
1244 1243
         example).
1245 1244
 
1246 1245
         'names' is the path of names to travle, 'opts' is the model Options we
1247  
-        start the name resolving from, 'allow_many' and 'allow_explicit_fk'
1248  
-        are as for setup_joins().
  1246
+        start the name resolving from, 'allow_many' is as for setup_joins().
1249 1247
 
1250 1248
         Returns a list of PathInfo tuples. In addition returns the final field
1251 1249
         (the last used join field), and target (which is a field guaranteed to
@@ -1258,17 +1256,9 @@ def names_to_path(self, names, opts, allow_many, allow_explicit_fk):
1258 1256
             try:
1259 1257
                 field, model, direct, m2m = opts.get_field_by_name(name)
1260 1258
             except FieldDoesNotExist:
1261  
-                for f in opts.fields:
1262  
-                    if allow_explicit_fk and name == f.attname:
1263  
-                        # XXX: A hack to allow foo_id to work in values() for
1264  
-                        # backwards compatibility purposes. If we dropped that
1265  
-                        # feature, this could be removed.
1266  
-                        field, model, direct, m2m = opts.get_field_by_name(f.name)
1267  
-                        break
1268  
-                else:
1269  
-                    available = opts.get_all_field_names() + list(self.aggregate_select)
1270  
-                    raise FieldError("Cannot resolve keyword %r into field. "
1271  
-                                     "Choices are: %s" % (name, ", ".join(available)))
  1259
+                available = opts.get_all_field_names() + list(self.aggregate_select)
  1260
+                raise FieldError("Cannot resolve keyword %r into field. "
  1261
+                                 "Choices are: %s" % (name, ", ".join(available)))
1272 1262
             # Check if we need any joins for concrete inheritance cases (the
1273 1263
             # field lives in parent, but we are currently in one of its
1274 1264
             # children)
@@ -1314,7 +1304,7 @@ def names_to_path(self, names, opts, allow_many, allow_explicit_fk):
1314 1304
         return path, final_field, targets
1315 1305
 
1316 1306
     def setup_joins(self, names, opts, alias, can_reuse=None, allow_many=True,
1317  
-                    allow_explicit_fk=False, outer_if_first=False):
  1307
+                    outer_if_first=False):
1318 1308
         """
1319 1309
         Compute the necessary table joins for the passage through the fields
1320 1310
         given in 'names'. 'opts' is the Options class for the current model
@@ -1329,9 +1319,6 @@ def setup_joins(self, names, opts, alias, can_reuse=None, allow_many=True,
1329 1319
         If 'allow_many' is False, then any reverse foreign key seen will
1330 1320
         generate a MultiJoin exception.
1331 1321
 
1332  
-        The 'allow_explicit_fk' controls if field.attname is allowed in the
1333  
-        lookups.
1334  
-
1335 1322
         Returns the final field involved in the joins, the target field (used
1336 1323
         for any 'where' constraint), the final 'opts' value, the joins and the
1337 1324
         field path travelled to generate the joins.
@@ -1345,7 +1332,7 @@ def setup_joins(self, names, opts, alias, can_reuse=None, allow_many=True,
1345 1332
         joins = [alias]
1346 1333
         # First, generate the path for the names
1347 1334
         path, final_field, targets = self.names_to_path(
1348  
-            names, opts, allow_many, allow_explicit_fk)
  1335
+            names, opts, allow_many)
1349 1336
         # Then, add the path to the query's joins. Note that we can't trim
1350 1337
         # joins at this stage - we will need the information about join type
1351 1338
         # of the trimmed joins.
3  tests/lookup/tests.py
@@ -470,7 +470,8 @@ def test_error_messages(self):
470 470
             self.fail('FieldError not raised')
471 471
         except FieldError as ex:
472 472
             self.assertEqual(str(ex), "Cannot resolve keyword 'pub_date_year' "
473  
-                             "into field. Choices are: author, headline, id, pub_date, tag")
  473
+                             "into field. Choices are: author, author_id, headline, "
  474
+                             "id, pub_date, tag")
474 475
         try:
475 476
             Article.objects.filter(headline__starts='Article')
476 477
             self.fail('FieldError not raised')

0 notes on commit c21e86a

Please sign in to comment.
Something went wrong with that request. Please try again.