| | 1 | |
| | 2 | == Admin change list view customization with ListColumn == |
| | 3 | |
| | 4 | Related ticket: #8054 Move method properties for admin list customisation to ModelAdmin |
| | 5 | |
| | 6 | This ticket adds: |
| | 7 | |
| | 8 | * beatifull API to admin change list customization |
| | 9 | |
| | 10 | * ability to customize and localize 3rd-party application without fork it |
| | 11 | |
| | 12 | * ability to apply custom template filters on field value or model method returned value without any magic |
| | 13 | |
| | 14 | * ability to specify witch field wiil be used in order for column based on model method |
| | 15 | |
| | 16 | |
| | 17 | ==== ListColumn API ==== |
| | 18 | |
| | 19 | Arguments: |
| | 20 | |
| | 21 | * field_name - model field or model method name |
| | 22 | |
| | 23 | Keyword arguments: |
| | 24 | |
| | 25 | |
| | 26 | * header - change list column header. If not provided standard algoritm to header column calculation will be used. |
| | 27 | |
| | 28 | * filter - template filters will be apllied to value on output. |
| | 29 | |
| | 30 | * load_filters - list of required template tags libraries. This libraries will be load before apply template filters to output value. |
| | 31 | |
| | 32 | * order_field - specify witch field will be used to prowide order by this column. |
| | 33 | |
| | 34 | |
| | 35 | |
| | 36 | ==== Example ==== |
| | 37 | |
| | 38 | Current way to admin change list view customization: |
| | 39 | |
| | 40 | {{{ |
| | 41 | class Account(models.Model): |
| | 42 | foo = model.BooleanField(...) |
| | 43 | bar = model.CharField(...) |
| | 44 | baz = model.CharField(...) |
| | 45 | bonk = model.CharField(...) |
| | 46 | ends = models.DateTimeField() |
| | 47 | |
| | 48 | def get_bar_column(self): |
| | 49 | return ... |
| | 50 | get_bar_column.allow_tags = True |
| | 51 | |
| | 52 | def timeuntil_ends(): |
| | 53 | return ... |
| | 54 | |
| | 55 | |
| | 56 | class AccountAdmin(admin.ModelAdmin): |
| | 57 | list_display = ['foo', 'get_bar_column', 'baz', 'bonk', 'timeuntil_ends'] |
| | 58 | |
| | 59 | }}} |
| | 60 | |
| | 61 | Proposed way: |
| | 62 | |
| | 63 | {{{ |
| | 64 | class Account(models.Model): |
| | 65 | foo = model.BooleanField(...) |
| | 66 | bar = model.CharField(...) |
| | 67 | baz = model.CharField(...) |
| | 68 | bonk = model.CharField(...) |
| | 69 | ends = models.DateTimeField() |
| | 70 | |
| | 71 | def get_bar(self): |
| | 72 | return ... |
| | 73 | |
| | 74 | class AccountAdmin(admin.ModelAdmin): |
| | 75 | list_display = [ |
| | 76 | admin.ListColumn('foo', header='Foo Description', filter='boolean'), |
| | 77 | admin.ListColumn('get_bar', filter='safe', order_field='bar'), |
| | 78 | 'baz', |
| | 79 | 'bonk', |
| | 80 | admin.ListColumn('ends', filter='timeuntil', header="Ending in") |
| | 81 | ] |
| | 82 | |
| | 83 | }}} |