Задать вопрос

Тел: +7 965 3737 888

420

Просмотров

0

Ответов

DRY custom ModelAdmin.list_display methods with a decorator

<p>If you add a lot of custom ModelAdmin methods to list_display like I do, you know it can require a lot of repetition. Notice how adding 'checkbox' to list_display requires typing the method name 4 times:</p>
class ExampleAdmin(admin.ModelAdmin):
    list_display = ['checkbox', '__str__']

    def checkbox(self, object):
        return '&lt;input type="checkbox" value="%s"/&gt;' % object.pk
    checkbox.short_description = mark_safe('&amp;#x2713;')
    checkbox.allow_tags = True

<p>Using this decorator, the name only needs to be typed once:</p>
class ExampleAdmin(admin.ModelAdmin):
    list_display = ['__str__']

    @add(list_display, mark_safe('&amp;#x2713;'), 0, allow_tags=True)
    def checkbox(self, object):
        return '&lt;input type="checkbox" value="%s"/&gt;' % object.pk

Вопрос полезен? Да0/Нет0
file_3747.py(1.3Кб)
None