Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 years ago

#7503 closed (fixed)

Allow list_display to take functions

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

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@… 6 years ago.

Download all attachments as: .zip

Change History (9)

Changed 6 years ago by qmanic@…

comment:1 Changed 6 years ago by mrts

  • Keywords someday/maybe added
  • milestone set to post-1.0
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 6 years ago by ericholscher

  • Triage Stage changed from Unreviewed to Someday/Maybe

comment:3 Changed 6 years ago by mtredinnick

  • Keywords someday/maybe removed
  • milestone changed from post-1.0 to 1.0
  • Triage Stage changed from Someday/Maybe to Accepted
  • Version changed from newforms-admin to SVN

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

comment:4 Changed 6 years ago by brosner

  • Owner changed from nobody to brosner
  • Status changed from new to assigned

comment:5 Changed 6 years ago by brosner

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 Changed 6 years ago by tobias

  • Resolution fixed deleted
  • Status changed from closed to reopened

[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 Changed 6 years ago by brosner

  • Resolution set to fixed
  • Status changed from reopened to closed

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

comment:8 Changed 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.