Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #2301 -- Added list_display_links option to 'class Admin', whic…

…h regulates which fields in the change list have links. Thanks, kilian

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3307 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c1847294b8c1b97993a945d43e50c56c0486ceed 1 parent 5ce4a56
Adrian Holovaty authored
1  AUTHORS
@@ -72,6 +72,7 @@ answer newbie questions, and generally made Django that much better:
72 72
     junzhang.jn@gmail.com
73 73
     Russell Keith-Magee <freakboy@iinet.net.au>
74 74
     Garth Kidd <http://www.deadlybloodyserious.com/>
  75
+    kilian <kilian.cavalotti@lip6.fr>
75 76
     Sune Kirkeby <http://ibofobi.dk/>
76 77
     Cameron Knight (ckknight)
77 78
     Bruce Kroeze <http://coderseye.com/>
3  django/contrib/admin/templatetags/admin_list.py
@@ -165,7 +165,8 @@ def items_for_result(cl, result):
165 165
                 result_repr = escape(str(field_val))
166 166
         if result_repr == '':
167 167
             result_repr = '&nbsp;'
168  
-        if first: # First column is a special case
  168
+        # If list_display_links not defined, add the link tag to the first field
  169
+        if (first and not cl.lookup_opts.admin.list_display_links) or field_name in cl.lookup_opts.admin.list_display_links: 
169 170
             first = False
170 171
             url = cl.url_for_result(result)
171 172
             result_id = str(getattr(result, pk)) # str() is needed in case of 23L (long ints)
13  django/core/management.py
@@ -936,6 +936,19 @@ def get_validation_errors(outfile, app=None):
936 936
                         else:
937 937
                             if isinstance(f, models.ManyToManyField):
938 938
                                 e.add(opts, '"admin.list_display" doesn\'t support ManyToManyFields (%r).' % fn)
  939
+                # list_display_links
  940
+                if opts.admin.list_display_links and not opts.admin.list_display:
  941
+                    e.add(opts, '"admin.list_display" must be defined for "admin.list_display_links" to be used.')
  942
+                if not isinstance(opts.admin.list_display_links, (list, tuple)):
  943
+                    e.add(opts, '"admin.list_display_links", if given, must be set to a list or tuple.')
  944
+                else:
  945
+                    for fn in opts.admin.list_display_links:
  946
+                        try:
  947
+                            f = opts.get_field(fn)
  948
+                        except models.FieldDoesNotExist:
  949
+                            e.add(opts, '"admin.list_filter" refers to %r, which isn\'t a field.' % fn)
  950
+                        if fn not in opts.admin.list_display:
  951
+                            e.add(opts, '"admin.list_display_links" refers to %r, which is not defined in "admin.list_display".' % fn)
939 952
                 # list_filter
940 953
                 if not isinstance(opts.admin.list_filter, (list, tuple)):
941 954
                     e.add(opts, '"admin.list_filter", if given, must be set to a list or tuple.')
3  django/db/models/options.py
@@ -199,12 +199,13 @@ def has_field_type(self, field_type, follow=None):
199 199
         return self._field_types[field_type]
200 200
 
201 201
 class AdminOptions(object):
202  
-    def __init__(self, fields=None, js=None, list_display=None, list_filter=None,
  202
+    def __init__(self, fields=None, js=None, list_display=None, list_display_links=None, list_filter=None,
203 203
         date_hierarchy=None, save_as=False, ordering=None, search_fields=None,
204 204
         save_on_top=False, list_select_related=False, manager=None, list_per_page=100):
205 205
         self.fields = fields
206 206
         self.js = js or []
207 207
         self.list_display = list_display or ['__str__']
  208
+        self.list_display_links = list_display_links or []
208 209
         self.list_filter = list_filter or []
209 210
         self.date_hierarchy = date_hierarchy
210 211
         self.save_as, self.ordering = save_as, ordering
27  docs/model-api.txt
@@ -1225,6 +1225,33 @@ A few special cases to note about ``list_display``:
1225 1225
                   return self.birthday.strftime('%Y')[:3] + "0's"
1226 1226
               decade_born_in.short_description = 'Birth decade'
1227 1227
 
  1228
+``list_display_links``
  1229
+----------------------
  1230
+
  1231
+Set ``list_display_links`` to control which fields in ``list_display`` should
  1232
+be linked to the "change" page for an object.
  1233
+
  1234
+By default, the change list page will link the first column -- the first field
  1235
+specified in ``list_display`` -- to the change page for each item. But
  1236
+``list_display_links`` lets you change which columns are linked. Set
  1237
+``list_display_links`` to a list or tuple of field names (in the same format as
  1238
+``list_display``) to link.
  1239
+
  1240
+``list_display_links`` can specify one or many field names. As long as the
  1241
+field names appear in ``list_display``, Django doesn't care how many (or how
  1242
+few) fields are linked. The only requirement is: If you want to use
  1243
+``list_display_links``, you must define ``list_display``.
  1244
+
  1245
+In this example, the ``first_name`` and ``last_name`` fields will be linked on
  1246
+the change list page::
  1247
+
  1248
+    class Admin:
  1249
+        list_display = ('first_name', 'last_name', 'birthday')
  1250
+        list_display_links = ('first_name', 'last_name')
  1251
+
  1252
+Finally, note that in order to use ``list_display_links``, you must define
  1253
+``list_display``, too.
  1254
+
1228 1255
 ``list_filter``
1229 1256
 ---------------
1230 1257
 

0 notes on commit c184729

Please sign in to comment.
Something went wrong with that request. Please try again.