Ticket #3288: 3288.diff
File 3288.diff, 3.4 KB (added by , 17 years ago) |
---|
-
django/db/models/query.py
501 501 502 502 # Add additional tables and WHERE clauses based on select_related. 503 503 if self._select_related: 504 fill_table_cache( opts, select, tables, where,504 fill_table_cache(self.model, select, tables, where, 505 505 old_prefix=opts.db_table, 506 506 cache_tables_seen=[opts.db_table], 507 507 max_depth=self._max_related_depth) … … 845 845 setattr(obj, f.get_cache_name(), rel_obj) 846 846 return obj, index_end 847 847 848 def fill_table_cache( opts, select, tables, where, old_prefix, cache_tables_seen, max_depth=0, cur_depth=0):848 def fill_table_cache(cls, select, tables, where, old_prefix, cache_tables_seen, max_depth=0, cur_depth=0): 849 849 """ 850 850 Helper function that recursively populates the select, tables and where (in 851 851 place) for select_related queries. … … 855 855 if max_depth and cur_depth > max_depth: 856 856 return None 857 857 858 opts = cls._meta 858 859 qn = connection.ops.quote_name 859 860 for f in opts.fields: 860 if f.rel and not f.null: 861 # we only visit FKs, and exclude those that can be null and those who point to our own Model 862 if f.rel and not f.null and f.rel.to != cls: 861 863 db_table = f.rel.to._meta.db_table 862 864 if db_table not in cache_tables_seen: 863 865 tables.append(qn(db_table)) … … 869 871 where.append('%s.%s = %s.%s' % \ 870 872 (qn(old_prefix), qn(f.column), qn(db_table), qn(f.rel.get_related_field().column))) 871 873 select.extend(['%s.%s' % (qn(db_table), qn(f2.column)) for f2 in f.rel.to._meta.fields]) 872 fill_table_cache(f.rel.to ._meta, select, tables, where, db_table, cache_tables_seen, max_depth, cur_depth+1)874 fill_table_cache(f.rel.to, select, tables, where, db_table, cache_tables_seen, max_depth, cur_depth+1) 873 875 874 876 def parse_lookup(kwarg_items, opts): 875 877 # Helper function that handles converting API kwargs -
tests/modeltests/select_related/models.py
75 75 obj.save() 76 76 parent = obj 77 77 78 # tests that select_related doesn't follow this kind of FK 79 class Foo(models.Model): 80 parent = models.ForeignKey('self') 81 82 class ALoop(models.Model): 83 b = models.ForeignKey('BLoop') 84 85 class BLoop(models.Model): 86 a = models.ForeignKey('BLoop') 87 78 88 __test__ = {'API_TESTS':""" 79 89 80 90 # Set up. … … 83 93 >>> from django.conf import settings 84 94 >>> settings.DEBUG = True 85 95 96 # simply verify that it's possible en enable select related on this model 97 >>> Foo.objects.all().select_related() 98 [] 99 100 # simply verify that it's possible en enable select related on this model 101 >>> ALoop.objects.all().select_related() 102 [] 103 >>> BLoop.objects.all().select_related() 104 [] 105 86 106 >>> create_tree("Eukaryota Animalia Anthropoda Insecta Diptera Drosophilidae Drosophila melanogaster") 87 107 >>> create_tree("Eukaryota Animalia Chordata Mammalia Primates Hominidae Homo sapiens") 88 108 >>> create_tree("Eukaryota Plantae Magnoliophyta Magnoliopsida Fabales Fabaceae Pisum sativum")