Ticket #10414: 10414_selectRelatedInvalidFields.diff
File 10414_selectRelatedInvalidFields.diff, 3.4 KB (added by , 14 years ago) |
---|
-
django/db/models/sql/compiler.py
5 5 from django.db.models.sql.datastructures import EmptyResultSet 6 6 from django.db.models.sql.expressions import SQLEvaluator 7 7 from django.db.models.sql.query import get_proxied_model, get_order_dir, \ 8 select_related_descend, Query 8 select_related_descend, Query, InvalidQuery 9 9 10 10 class SQLCompiler(object): 11 11 def __init__(self, query, connection, using): … … 516 516 517 517 # Setup for the case when only particular related fields should be 518 518 # included in the related selection. 519 fields_found = set() 519 520 if requested is None: 520 521 if isinstance(self.query.select_related, dict): 521 522 requested = self.query.select_related … … 526 527 for f, model in opts.get_fields_with_model(): 527 528 if not select_related_descend(f, restricted, requested): 528 529 continue 530 fields_found.add(f.name) 529 531 # The "avoid" set is aliases we want to avoid just for this 530 532 # particular branch of the recursion. They aren't permanently 531 533 # forbidden from reuse in the related selection tables (which is … … 591 593 used, next, restricted, new_nullable, dupe_set, avoid) 592 594 593 595 if restricted: 596 fields_not_found = set(requested.keys()).difference(fields_found) 597 if fields_not_found: 598 raise InvalidQuery("Invalid field name(s) %s in select_related() %s" % 599 (",".join(list(fields_not_found)),requested)) 594 600 related_fields = [ 595 601 (o.field, o.model) 596 602 for o in opts.get_all_related_objects() -
tests/modeltests/select_related/tests.py
1 1 from django.test import TestCase 2 from django.db.models.sql.query import InvalidQuery 2 3 3 4 from models import Domain, Kingdom, Phylum, Klass, Order, Family, Genus, Species 4 5 … … 142 143 ['Drosophilidae', 'Hominidae', 'Fabaceae', 'Amanitacae']) 143 144 self.assertNumQueries(1, test) 144 145 146 def test_certain_fields_fails(self): 147 """ 148 In this case we pass some invalid field name(s). InvalidQuery 149 exception is expected. 150 """ 151 def test(*args): 152 world = list(Species.objects.select_related(*args)) 153 154 self.assertNumQueries(1, test, 'genus', 'genus__family') 155 self.assertRaises(InvalidQuery, test, 'genusFAIL', 'genus__family') 156 self.assertRaises(InvalidQuery, test, 'genus', 'genus__familyFAIL') 157 self.assertRaises(InvalidQuery, test, 'genusFAIL', 'genusFAIL__family') 158 159 self.assertNumQueries(1, test, 'genus__family__order') 160 self.assertRaises(InvalidQuery, test, 'genusFAIL__family__order') 161 self.assertRaises(InvalidQuery, test, 'genus__familyFAIL__order') 162 self.assertRaises(InvalidQuery, test, 'genus__family__orderFAIL') 163 164 145 165 def test_more_certain_fields(self): 146 166 """ 147 167 In this case, we explicitly say to select the 'genus' and