diff -uri /home/Philippe/Django-0.96/django/db/models/query.py ./django/db/models/query.py
old
|
new
|
|
488 | 488 | |
489 | 489 | # Add additional tables and WHERE clauses based on select_related. |
490 | 490 | if self._select_related: |
491 | | fill_table_cache(opts, select, tables, where, |
| 491 | fill_table_cache(self.model, select, tables, where, |
492 | 492 | old_prefix=opts.db_table, |
493 | 493 | cache_tables_seen=[opts.db_table], |
494 | 494 | max_depth=self._max_related_depth) |
… |
… |
|
753 | 753 | setattr(obj, f.get_cache_name(), rel_obj) |
754 | 754 | return obj, index_end |
755 | 755 | |
756 | | def fill_table_cache(opts, select, tables, where, old_prefix, cache_tables_seen, max_depth=0, cur_depth=0): |
| 756 | def fill_table_cache(cls, select, tables, where, old_prefix, cache_tables_seen, max_depth=0, cur_depth=0): |
757 | 757 | """ |
758 | 758 | Helper function that recursively populates the select, tables and where (in |
759 | 759 | place) for select_related queries. |
… |
… |
|
763 | 763 | if max_depth and cur_depth > max_depth: |
764 | 764 | return None |
765 | 765 | |
| 766 | opts = cls._meta |
766 | 767 | qn = backend.quote_name |
767 | 768 | for f in opts.fields: |
768 | | if f.rel and not f.null: |
| 769 | # we only visit FKs, and exclude those that can be null and those who point to our own Model |
| 770 | if f.rel and not f.null and f.rel.to != cls: |
769 | 771 | db_table = f.rel.to._meta.db_table |
770 | 772 | if db_table not in cache_tables_seen: |
771 | 773 | tables.append(qn(db_table)) |
… |
… |
|
777 | 779 | where.append('%s.%s = %s.%s' % \ |
778 | 780 | (qn(old_prefix), qn(f.column), qn(db_table), qn(f.rel.get_related_field().column))) |
779 | 781 | select.extend(['%s.%s' % (qn(db_table), qn(f2.column)) for f2 in f.rel.to._meta.fields]) |
780 | | fill_table_cache(f.rel.to._meta, select, tables, where, db_table, cache_tables_seen, max_depth, cur_depth+1) |
| 782 | fill_table_cache(f.rel.to, select, tables, where, db_table, cache_tables_seen, max_depth, cur_depth+1) |
781 | 783 | |
782 | 784 | def parse_lookup(kwarg_items, opts): |
783 | 785 | # Helper function that handles converting API kwargs |
diff -uri /home/Philippe/Django-0.96/tests/modeltests/select_related/models.py ./tests/modeltests/select_related/models.py
old
|
new
|
|
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") |