From 181ee4ae3175f2c7dfd89dd3418dfe81cc135a51 Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Sat, 5 Nov 2005 18:14:17 +0000 Subject: [PATCH] Added meta.Admin list_select_related option, which specifies to use select_related in the change-list query git-svn-id: http://code.djangoproject.com/svn/django/trunk@1092 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/views/main.py | 25 ++++++++++++++----------- django/core/meta/fields.py | 3 ++- 2 files changed, 16 insertions(+), 12 deletions(-) diff --git a/django/contrib/admin/views/main.py b/django/contrib/admin/views/main.py index e3dd5afdcf..b188f58947 100644 --- a/django/contrib/admin/views/main.py +++ b/django/contrib/admin/views/main.py @@ -135,17 +135,20 @@ def change_list(request, app_label, module_name): f = lookup_opts.get_field(order_field) rel_ordering = f.rel.to.ordering and f.rel.to.ordering[0] or f.rel.to.pk.column lookup_order_field = '%s.%s' % (f.rel.to.db_table, rel_ordering) - # Use select_related if one of the list_display options is a field with a - # relationship. - for field_name in lookup_opts.admin.list_display: - try: - f = lookup_opts.get_field(field_name) - except meta.FieldDoesNotExist: - pass - else: - if isinstance(f.rel, meta.ManyToOne): - lookup_params['select_related'] = True - break + if lookup_opts.admin.list_select_related: + lookup_params['select_related'] = True + else: + # Use select_related if one of the list_display options is a field with + # a relationship. + for field_name in lookup_opts.admin.list_display: + try: + f = lookup_opts.get_field(field_name) + except meta.FieldDoesNotExist: + pass + else: + if isinstance(f.rel, meta.ManyToOne): + lookup_params['select_related'] = True + break lookup_params['order_by'] = ((order_type == 'desc' and '-' or '') + lookup_order_field,) if lookup_opts.admin.search_fields and query: or_queries = [] diff --git a/django/core/meta/fields.py b/django/core/meta/fields.py index a15432dcd2..e23e6cda88 100644 --- a/django/core/meta/fields.py +++ b/django/core/meta/fields.py @@ -730,7 +730,7 @@ class OneToOne(ManyToOne): class Admin: def __init__(self, fields=None, js=None, list_display=None, list_filter=None, date_hierarchy=None, - save_as=False, ordering=None, search_fields=None, save_on_top=False): + save_as=False, ordering=None, search_fields=None, save_on_top=False, list_select_related=False): self.fields = fields self.js = js or [] self.list_display = list_display or ['__repr__'] @@ -739,6 +739,7 @@ class Admin: self.save_as, self.ordering = save_as, ordering self.search_fields = search_fields or [] self.save_on_top = save_on_top + self.list_select_related = list_select_related def get_field_objs(self, opts): """