Skip to main content

Django’s admin accepts callables for list_display and readonly_fields. In order to customize these callables (e.g. annotate them with a description) django requires developer to set an attribute on a callable (e.g. ‘short_description’). This is a simple approach and it works.

But it is easy to make a typo or to forget what attribute should be set on a callable because there is no autocompletion.

This app provides an alternative (decorator) syntax for that + a couple of other decorators useful for customizing django admin.

Installation

pip install django-admin-decorators

Usage

from django.contrib import admin
from admin_decorators import (short_description, limit_width, boolean,
                               apply_filter, order_field, allow_tags)
from myapp.models import MyModel

class MyModelAdmin(admin.ModelAdmin):
    list_display = 'name', '_text', 'has_huge_text', 'html_url'
    readonly_fields = ['name', 'html_url']

    @short_description('The text limited to 100 chars')
    @order_field('text')
    @limit_width(100)
    def _text(self, obj):
        return obj.text

    @boolean
    @short_description('The text is huge')
    def has_huge_text(self, obj):
        return len(obj.text) > 100

    @short_description('link')
    @order_field('url')
    @allow_tags
    @apply_filter('urlize')
    def html_url(self, obj):
        return obj.url

admin.site.register(MyModel, MyModelAdmin)

Note that allow_tags decorator marks result as safe so it will be html both in list_display and readonly_fields.

Take a look at source code for more decorators.

Development

Development happens at bitbucket and github.

If you’ve found a bug or have an idea for a new decorator feel free to open a ticket and/or send a pull request.

Release files for django-admin-decorators 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-admin-decorators 0.1
File Size Uploaded
django-admin-decorators-0.1.tar.gz 3.6 kB Details

Release files / django-admin-decorators-0.1.tar.gz

Download URL django-admin-decorators-0.1.tar.gz
Size 3.6 kB
Tags Source
SHA-256 checksum
How to use checksums
cdb2393fa9de145cbba91fb2cc7281ffab2e6becd294f2f19960e1711ff736bf
BLAKE2b-256 checksum
How to use checksums
e5078825af3c4edd247eda5332483dd1794f8e5e7b7868d88479ecfb620a96de
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1 This release

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page