Django

Code

Ticket #8054 (assigned)

Opened 2 years ago

Last modified 5 months ago

Move method properties for admin list customisation to ModelAdmin

Reported by: Daniel Pope <dan@mauveinternet.co.uk> Assigned to: brosner (accepted)
Milestone: Component: django.contrib.admin
Version: SVN Keywords:
Cc: andy@andybak.net Triage Stage: Accepted
Has patch: 1 Needs documentation: 1
Needs tests: 1 Patch needs improvement: 1

Description

The current approach for customising list_display seems contrary to the spirit of newforms-admin. Method properties such as .short_description, .boolean and .allow_tags rarely belong in the model definition.

In fact, I noted that these options, for customising the look of the list column, duplicate filter functionality. .allow_tags is exactly |safe; |boolean could be a filter provided by django.contrib.admin.templatetags. The case is much more convincing when you take into account user-defined filters: if I already have a filter for presenting values for user consumption, I should not need to provide a model method to replicate or apply that filter.

I propose a syntax like this:

class AccountAdmin(admin.ModelAdmin):
    foo_column = admin.ListColumn('foo', heading='Foo Description', filter='boolean')
    get_bar_column = admin.ListColumn('get_bar', filter='safe', order_field='bar')

    list_display = [foo_column, get_bar_column, 'baz', 'bonk']

Filter would be specified in exactly template syntax for convenience, so

   get_bar_column = admin.ListColumn('get_bar', filter='truncatewords:"2"|slugify')

would render exactly the same as

{{row.get_bar|truncatewords:"2"|slugify}}

Attachments

8054-list-column.diff (17.0 kB) - added by dan@mauveinternet.co.uk on 08/11/08 18:27:32.
Initial patch
8054-list-column.2.diff (18.0 kB) - added by Daniel Pope <dan@mauveinternet.co.uk> on 08/11/08 19:02:05.
Fixed sorting with ListColumns?

Change History

08/08/08 12:26:38 changed by ericholscher

  • needs_better_patch changed.
  • stage changed from Unreviewed to Design decision needed.
  • needs_tests changed.
  • needs_docs changed.

08/11/08 03:46:35 changed by Daniel Pope <dan@mauveinternet.co.uk>

  • component changed from Uncategorized to Admin interface.

The reason I suggested filters be specified like this was because I thought it would be simpler for users to re-use this element of template syntax than to import the actual filter functions, especially because these would have to be curried to pass arguments.

ListColumn would probably also need to take a load_filters=[] kwarg to address templatetag library loading.

08/11/08 18:27:32 changed by dan@mauveinternet.co.uk

  • attachment 8054-list-column.diff added.

Initial patch

08/11/08 18:42:55 changed by Daniel Pope <dan@mauveinternet.co.uk>

  • needs_better_patch set to 1.
  • has_patch set to 1.
  • needs_tests set to 1.
  • needs_docs set to 1.

I have tried coding up an implementation of this ticket (exactly as described above except the kwarg is 'header' not 'heading'). Patch needs improvement as it isn't pep8-compliant.

08/11/08 18:47:14 changed by brosner

  • milestone set to post-1.0.

Not much time to get this in before feature-freeze.

08/11/08 19:02:05 changed by Daniel Pope <dan@mauveinternet.co.uk>

  • attachment 8054-list-column.2.diff added.

Fixed sorting with ListColumns?

08/13/08 12:45:31 changed by brosner

  • owner changed from nobody to brosner.
  • status changed from new to assigned.
  • stage changed from Design decision needed to Accepted.
  • milestone changed from post-1.0 to 1.0 beta.

I am going to take a step back and put this on 1.0-beta. However, the implementation is a bit too much for this. I want to just decouple the list_display methods from the model. It will be backward incompatible.

08/13/08 13:14:23 changed by mtredinnick

@brosner: allow list_display methods/functions to also exist on the model instance would be good, though. There are plenty of methods that naturally belong on models that are useful in the admin display.

08/13/08 14:19:09 changed by brosner

@malcolmt: Good point. I was just talking with Joseph and I was wondering about the naming of admin_order_field. If we have methods in ModelAdmin? that admin bit is redundant. Should that be dropped or maintained?

08/13/08 14:44:13 changed by Daniel Pope <dan@mauveinternet.co.uk>

Would you just search both the model instance and the ModelAdmin for callables matching the list_display column? My patch should be backwards compatible, so it could be backwards compatible with yours if you are implementing it like that.

This turned out to be quite a nice refactor, as it broke up the monolithic functions for rendering ChangeLists (actually they were template inclusion tags) into distinct stages of working out how to render each column, then what to render, then rendering it, so that's something else to be said for developing this post-1.0, not least the neat separation between content and presentation that the filters kwarg allows.

08/13/08 15:32:31 changed by brosner

@Daniel: I have sort of hijacked this ticket. You sort of described two different things one of which needs to get in before 1.0. The new API bits you have described above are worthy once we look at modified the API a bit more for the ChangeList stuff. The way I have it setup is it looks at the ModelAdmin first and if not found looks at the model level for non-field, non-callable (a new feature i am adding) in list_display.

08/13/08 17:59:31 changed by Daniel Pope <dan@mauveinternet.co.uk>

That's fine. So you'll also implement #7503?

Yes, I realise it was really two separate things. It started out as just noting that in ModelAdmin there's now space to tidy up the system of method attributes, but somewhere along the way my main motivation became wanting to apply template filters. I noticed it made a lot of sense not just for the existing modifiers I mentioned, but practical sense in several of my apps, and that's a much bigger win than just API pointling.

For instance, you can add useful columns entirely with ListColumn, for example:

class SpecialOffer(models.Model):
    name = models.CharField(...)
    ends = models.DateTimeField()

class SpecialOfferAdmin(admin.ModelAdmin):
    list_display = [
        'name',
        'ends',
         admin.ListColumn('ends', filter='timeuntil', heading="Ending in")
    ]

08/14/08 15:12:21 changed by brosner

(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.

08/14/08 15:13:50 changed by brosner

  • milestone changed from 1.0 beta to post-1.0.

Moving to post-1.0 for the API changing parts. This should be looked at with a ChangeList and templatetag refactor of the admin. Not sure when or how, but this is related.

02/25/09 13:51:44 changed by

  • milestone deleted.

Milestone post-1.0 deleted

10/16/09 03:41:41 changed by anonymous

  • cc set to andy@andybak.net.

Add/Change #8054 (Move method properties for admin list customisation to ModelAdmin)




Change Properties
Action