Opened 6 years ago

Closed 6 years ago

#30259 closed New feature (fixed)

Support the use of admin_order_field on properties in the admin.

Reported by: Tobias Wiese Owned by: Jani Tiainen
Component: contrib.admin Version: dev
Severity: Release blocker 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

The ModelAdmin class allows to specify properties from the model in list_display.

Normally non-fields cannot be sorted. This can be changed by setting admin_order_field. This doesn't work on properties, while it is possible to do so with short_description.

The code that checks for short_description checks if it handles a property and returns attr.fget.short_description if available.

The files that check for admin_order_field do not handle the case that it might be a property.

The relevant files are probably:

  • django/contrib/admin/views/main.py (GitHub)
  • django/contrib/admin/templatetags/admin_list.py ( GitHub)

Change History (10)

comment:1 by Jani Tiainen, 6 years ago

Owner: changed from nobody to Jani Tiainen
Status: newassigned
Triage Stage: UnreviewedAccepted

comment:2 by Jani Tiainen, 6 years ago

Has patch: set

comment:3 by Tobias Wiese, 6 years ago

Triage Stage: AcceptedReady for checkin

comment:4 by Mariusz Felisiak, 6 years ago

Summary: admin_order_field not working with propertiesSupport the use of admin_order_field on properties in the admin.
Version: 2.1master

comment:5 by Mariusz Felisiak, 6 years ago

Type: BugNew feature

comment:6 by Mariusz Felisiak <felisiak.mariusz@…>, 6 years ago

Resolution: fixed
Status: assignedclosed

In 044cc544:

Fixed #30259 -- Added support for admin_order_field attribute on properties in ModelAdmin.list_display.

comment:7 by Tim Graham, 6 years ago

Easy pickings: unset
Has patch: unset
Resolution: fixed
Severity: NormalRelease blocker
Status: closednew
Triage Stage: Ready for checkinAccepted

A selenium test fails after the patch: admin_views.tests.SeleniumTests.test_list_editable_raw_id_fields:

Traceback (most recent call last):
  File "/home/tim/code/django/django/core/handlers/exception.py", line 34, in inner
    response = get_response(request)
  File "/home/tim/code/django/django/core/handlers/base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/home/tim/code/django/django/core/handlers/base.py", line 143, in _get_response
    response = response.render()
  File "/home/tim/code/django/django/template/response.py", line 106, in render
    self.content = self.rendered_content
  File "/home/tim/code/django/django/template/response.py", line 83, in rendered_content
    content = template.render(context, self._request)
  File "/home/tim/code/django/django/template/backends/django.py", line 61, in render
    return self.template.render(context)
  File "/home/tim/code/django/django/template/base.py", line 171, in render
    return self._render(context)
  File "/home/tim/code/django/django/test/utils.py", line 95, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/tim/code/django/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/tim/code/django/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/tim/code/django/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/home/tim/code/django/django/test/utils.py", line 95, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/tim/code/django/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/tim/code/django/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/tim/code/django/django/template/loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "/home/tim/code/django/django/test/utils.py", line 95, in instrumented_test_render
    return self.nodelist.render(context)
  File "/home/tim/code/django/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/tim/code/django/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/tim/code/django/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/home/tim/code/django/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/tim/code/django/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/tim/code/django/django/template/loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "/home/tim/code/django/django/template/base.py", line 937, in render
    bit = node.render_annotated(context)
  File "/home/tim/code/django/django/template/base.py", line 904, in render_annotated
    return self.render(context)
  File "/home/tim/code/django/django/contrib/admin/templatetags/base.py", line 33, in render
    return super().render(context)
  File "/home/tim/code/django/django/template/library.py", line 214, in render
    _dict = self.func(*resolved_args, **resolved_kwargs)
  File "/home/tim/code/django/django/contrib/admin/templatetags/admin_list.py", line 332, in result_list
    headers = list(result_headers(cl))
  File "/home/tim/code/django/django/contrib/admin/templatetags/admin_list.py", line 133, in result_headers
    admin_order_field = getattr(attr.fget, 'admin_order_field')
AttributeError: 'function' object has no attribute 'admin_order_field'

in reply to:  7 comment:8 by Jani Tiainen, 6 years ago

Replying to Tim Graham:

A selenium test fails after the patch: admin_views.tests.SeleniumTests.test_list_editable_raw_id_fields:

  File "/home/tim/code/django/django/contrib/admin/templatetags/admin_list.py", line 133, in result_headers
    admin_order_field = getattr(attr.fget, 'admin_order_field')
AttributeError: 'function' object has no attribute 'admin_order_field'

Apparently that getattr should be:

admin_order_field = getattr(attr.fget, 'admin_order_field', None)

comment:9 by Mariusz Felisiak, 6 years ago

Has patch: set

comment:10 by GitHub <noreply@…>, 6 years ago

Resolution: fixed
Status: newclosed

In 7cbcf2e2:

Fixed #30259 -- Fixed crash of admin views when properties don't have admin_order_field attribute.

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