831 | | for f in klass._meta.fields: |
832 | | if f.rel and not f.null: |
833 | | cached_row = get_cached_row(f.rel.to, row, index_end, max_depth, cur_depth+1) |
834 | | if cached_row: |
835 | | rel_obj, index_end = cached_row |
836 | | setattr(obj, f.get_cache_name(), rel_obj) |
| 836 | for f in fields_to_join.iterkeys(): |
| 837 | cached_row = get_cached_row(f.rel.to, row, index_end, fields_to_join[f], max_depth, cur_depth+1) |
| 838 | if cached_row: |
| 839 | rel_obj, index_end = cached_row |
| 840 | setattr(obj, f.get_cache_name(), rel_obj) |
839 | | def fill_table_cache(opts, select, tables, where, old_prefix, cache_tables_seen, max_depth=0, cur_depth=0): |
| 843 | def get_select_related_fields(opts, fields=None): |
| 844 | """ |
| 845 | Helper function that returns a dictionary of fields for select_related() |
| 846 | """ |
| 847 | if fields is None: |
| 848 | fields_to_join = dict([(f, None) for f in opts.fields if f.rel and not f.null]) |
| 849 | else: |
| 850 | fields_for_lookup = dict([(f.name, f) for f in opts.fields if f.rel]) |
| 851 | fields_to_join = dict() |
| 852 | for f in fields: |
| 853 | path = f.split(LOOKUP_SEPARATOR) |
| 854 | try: |
| 855 | fn = fields_for_lookup[path[0]] |
| 856 | if fn not in fields_to_join: |
| 857 | fields_to_join[fn] = [] |
| 858 | if len(path) > 1: |
| 859 | fields_to_join[fn].append(LOOKUP_SEPARATOR.join(path[1:])) |
| 860 | except KeyError: |
| 861 | raise FieldDoesNotExist, '%s has no field named %s' % (opts.object_name, path[0]) |
| 862 | return fields_to_join |
| 863 | |
| 864 | def fill_table_cache(opts, select, tables, where, old_prefix, cache_tables_seen, max_depth=0, fields=None, cur_depth=0): |
850 | | for f in opts.fields: |
851 | | if f.rel and not f.null: |
852 | | db_table = f.rel.to._meta.db_table |
853 | | if db_table not in cache_tables_seen: |
854 | | tables.append(qn(db_table)) |
855 | | else: # The table was already seen, so give it a table alias. |
856 | | new_prefix = '%s%s' % (db_table, len(cache_tables_seen)) |
857 | | tables.append('%s %s' % (qn(db_table), qn(new_prefix))) |
858 | | db_table = new_prefix |
859 | | cache_tables_seen.append(db_table) |
860 | | where.append('%s.%s = %s.%s' % \ |
861 | | (qn(old_prefix), qn(f.column), qn(db_table), qn(f.rel.get_related_field().column))) |
862 | | select.extend(['%s.%s' % (qn(db_table), qn(f2.column)) for f2 in f.rel.to._meta.fields]) |
863 | | fill_table_cache(f.rel.to._meta, select, tables, where, db_table, cache_tables_seen, max_depth, cur_depth+1) |
| 879 | for f in fields_to_join.iterkeys(): |
| 880 | db_table = f.rel.to._meta.db_table |
| 881 | if db_table not in cache_tables_seen: |
| 882 | tables.append(qn(db_table)) |
| 883 | else: # The table was already seen, so give it a table alias. |
| 884 | new_prefix = '%s%s' % (db_table, len(cache_tables_seen)) |
| 885 | tables.append('%s %s' % (qn(db_table), qn(new_prefix))) |
| 886 | db_table = new_prefix |
| 887 | cache_tables_seen.append(db_table) |
| 888 | where.append('%s.%s = %s.%s' % \ |
| 889 | (qn(old_prefix), qn(f.column), qn(db_table), qn(f.rel.get_related_field().column))) |
| 890 | select.extend(['%s.%s' % (qn(db_table), qn(f2.column)) for f2 in f.rel.to._meta.fields]) |
| 891 | fill_table_cache(f.rel.to._meta, select, tables, where, db_table, cache_tables_seen, max_depth, fields_to_join[f], cur_depth+1) |