Code

Ticket #1245: admin_views_main.diff

File admin_views_main.diff, 936 bytes (added by Jorge Gajon <gajon@…>, 8 years ago)

Fix lookup_order_field for OneToOne fields

Line 
1Index: django/contrib/admin/views/main.py
2===================================================================
3--- django/contrib/admin/views/main.py  (revision 2714)
4+++ django/contrib/admin/views/main.py  (working copy)
5@@ -199,8 +199,10 @@
6         except meta.FieldDoesNotExist:
7             pass
8         else:
9-            if isinstance(lookup_opts.get_field(order_field).rel, meta.ManyToOneRel):
10-                f = lookup_opts.get_field(order_field)
11+            f = lookup_opts.get_field(order_field)
12+            if isinstance(f.rel, meta.OneToOneRel):
13+                lookup_order_field = '%s' % (f.column,)
14+            elif isinstance(f.rel, meta.ManyToOneRel):
15                 rel_ordering = f.rel.to.ordering and f.rel.to.ordering[0] or f.rel.to.pk.column
16                 lookup_order_field = '%s.%s' % (f.rel.to.db_table, rel_ordering)
17         # Use select_related if one of the list_display options is a field with a