Giter Club home page Giter Club logo

django-admin-decorators's Introduction

django-admin-decorators

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 new decorator feel free to open a ticket and/or send a pull request.

django-admin-decorators's People

Contributors

kmike avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

hackaugusto

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.