Opened 16 years ago

Closed 16 years ago

Last modified 13 years ago

#7503 closed (fixed)

Allow list_display to take functions

Reported by: qmanic@… Owned by: Brian Rosner
Component: contrib.admin Version: dev
Severity: Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Just a thought - some of the apps I've been writing require me to add additional columns to the change list for models in the admin interface. The way to do this currently add a method name to list_display which gets looked up on the model and called, e.g.:

class Account(models.Model):
    def my_func(self):
        # do admin stuff here and return a string

...

class AccountAdmin(admin.ModelAdmin):
    list_display = ('user', 'my_func', 'get_user_email', 'status', )
    

The thing is, my_func is something that would only ever be used as part of the admin (e.g. I have one that is used to generate links to the admin page for a objects related by a ForeignKey) and I feel kind of dirty putting it on the model itself.

I've written up a hack-in-a-minute (i.e. needs work) patch that would allow you to do something like:

def my_func(object):
    # interrogate the object and return a string

class AccountAdmin(admin.ModelAdmin):
    list_display = ('user', my_func, 'get_user_email', 'status', )
    

Thoughts?

Attachments (1)

list_display_takes_function.patch (2.3 KB ) - added by qmanic@… 16 years ago.

Download all attachments as: .zip

Change History (9)

by qmanic@…, 16 years ago

comment:1 by mrts, 16 years ago

Keywords: someday/maybe added
milestone: post-1.0

No need to complicate things. -1, marking as someday/maybe.

comment:2 by Eric Holscher, 16 years ago

Triage Stage: UnreviewedSomeday/Maybe

comment:3 by Malcolm Tredinnick, 16 years ago

Keywords: someday/maybe removed
milestone: post-1.01.0
Triage Stage: Someday/MaybeAccepted
Version: newforms-adminSVN

Jacob and I just noticed and discussed this. Well worth doing and probably not very hard, so bumping to 1.0.

comment:4 by Brian Rosner, 16 years ago

Owner: changed from nobody to Brian Rosner
Status: newassigned

comment:5 by Brian Rosner, 16 years ago

Resolution: fixed
Status: assignedclosed

(In [8352]) Fixed #7503 -- Allow callables in list_display. This also does a lookup on the ModelAdmin for the method if the value is a string before looking on the model. Refs #8054. Thanks qmanic and Daniel Pope for tickets and patches.

comment:6 by Tobias McNulty, 16 years ago

Resolution: fixed
Status: closedreopened

[8352] breaks simple list display. to reproduce,

# models.py
class Site(models.Model):
	name = models.CharField(max_length=255)
	
	def __unicode__(self):
		return self.name

# admin.py
from django.contrib import admin
from visitdb import models as visitdb

admin.site.register(visitdb.Site)

go to /admin, and click on Site.

Environment:

Request Method: GET
Request URL: http://localhost:8000/django-admin/visitdb/site/
Django Version: 1.0-alpha_2-SVN-8352
Python Version: 2.5.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.admin',
 'django.contrib.humanize',
 'visitdb',
 'photologue']
Installed Middleware:
('visitdb.middleware.RequestAttributeSetup',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.middleware.doc.XViewMiddleware',
 'visitdb.middleware.StandardViewKwargsMiddleware')


Template error:
In template /home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/contrib/admin/templates/admin/change_list.html, error at line 34
   Caught an exception while rendering: local variable 'attr' referenced before assignment
   24 : {% if cl.has_filters %}


   25 : <div id="changelist-filter">


   26 : <h2>{% trans 'Filter' %}</h2>


   27 : {% for spec in cl.filter_specs %}


   28 :    {% admin_list_filter cl spec %}


   29 : {% endfor %}


   30 : </div>


   31 : {% endif %}


   32 : {% endblock %}


   33 : 


   34 : {% block result_list %} {% result_list cl %} {% endblock %}


   35 : {% block pagination %}{% pagination cl %}{% endblock %}


   36 : </div>


   37 : </div>


   38 : {% endblock %}


   39 : 

Traceback:
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/core/handlers/base.py" in get_response
  86.                 response = callback(request, *callback_args, **callback_kwargs)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/contrib/admin/sites.py" in root
  172.                 return self.model_page(request, *url.split('/', 2))
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/views/decorators/cache.py" in _wrapped_view_func
  44.         response = view_func(request, *args, **kwargs)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/contrib/admin/sites.py" in model_page
  189.         return admin_obj(request, rest_of_url)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/contrib/admin/options.py" in __call__
  267.             return self.changelist_view(request)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/contrib/admin/options.py" in changelist_view
  719.         ], context, context_instance=template.RequestContext(request))
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/shortcuts/__init__.py" in render_to_response
  18.     return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/loader.py" in render_to_string
  107.     return t.render(context_instance)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/__init__.py" in render
  176.         return self.nodelist.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/__init__.py" in render
  751.                 bits.append(self.render_node(node, context))
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/debug.py" in render_node
  71.             result = node.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/loader_tags.py" in render
  97.         return compiled_parent.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/__init__.py" in render
  176.         return self.nodelist.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/__init__.py" in render
  751.                 bits.append(self.render_node(node, context))
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/debug.py" in render_node
  71.             result = node.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/loader_tags.py" in render
  97.         return compiled_parent.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/__init__.py" in render
  176.         return self.nodelist.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/__init__.py" in render
  751.                 bits.append(self.render_node(node, context))
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/debug.py" in render_node
  71.             result = node.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/loader_tags.py" in render
  24.         result = self.nodelist.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/__init__.py" in render
  751.                 bits.append(self.render_node(node, context))
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/debug.py" in render_node
  71.             result = node.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/loader_tags.py" in render
  24.         result = self.nodelist.render(context)
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/__init__.py" in render
  751.                 bits.append(self.render_node(node, context))
File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/debug.py" in render_node
  81.             raise wrapped

Exception Type: TemplateSyntaxError at /django-admin/visitdb/site/
Exception Value: Caught an exception while rendering: local variable 'attr' referenced before assignment

Original Traceback (most recent call last):
  File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/debug.py", line 71, in render_node
    result = node.render(context)
  File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/template/__init__.py", line 898, in render
    dict = func(*args)
  File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/contrib/admin/templatetags/admin_list.py", line 235, in result_list
    'result_headers': list(result_headers(cl)),
  File "/home/tobias/caktus/eclipse-workspace/blueridge_visit_database/django/contrib/admin/templatetags/admin_list.py", line 110, in result_headers
    admin_order_field = getattr(attr, "admin_order_field", None)
UnboundLocalError: local variable 'attr' referenced before assignment

comment:7 by Brian Rosner, 16 years ago

Resolution: fixed
Status: reopenedclosed

Please don't re-open this ticket. Report the problem in a new one. I will look into it.

comment:8 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

Note: See TracTickets for help on using tickets.
Back to Top