All Projects → crccheck → Django Object Actions

crccheck / Django Object Actions

Licence: apache-2.0
A Django app for easily adding object tools in the Django admin

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Django Object Actions

Repoll
Redis管理平台Repoll,现已开源,基于redis3.x,支持单机、哨兵以及集群模式
Stars: ✭ 196 (-47.59%)
Mutual labels:  django, django-admin
Django Practice Book
《Django企业开发实战》已出版
Stars: ✭ 251 (-32.89%)
Mutual labels:  django, django-admin
Django Admin Env Notice
Visually distinguish environments in Django Admin
Stars: ✭ 207 (-44.65%)
Mutual labels:  django, django-admin
Awesome Django Admin
Curated List of Awesome Django Admin Panel Articles, Libraries/Packages, Books, Themes, Videos, Resources.
Stars: ✭ 356 (-4.81%)
Mutual labels:  django, django-admin
Django Admin Easy
Collection of admin fields and decorators to help to create computed or custom fields more friendly and easy way
Stars: ✭ 265 (-29.14%)
Mutual labels:  django, django-admin
Django Antd Tyadmin
类似 xadmin 的基于Model 快速生成前后台管理增删改查,筛选,搜索的后台管理自动化工具。Antd 界面好看现代化!前后端分离!无损二次开发!由Django Restful Framework 和 Ant Design Pro V4 驱动
Stars: ✭ 171 (-54.28%)
Mutual labels:  django, django-admin
Django Flat Responsive
📱 An extension for Django admin that makes interface mobile-friendly. Merged into Django 2.0
Stars: ✭ 249 (-33.42%)
Mutual labels:  django, django-admin
Django Polymorphic Tree
Polymorphic MPTT tree support for models
Stars: ✭ 152 (-59.36%)
Mutual labels:  django, django-admin
Django Fluent Dashboard
An improved django-admin-tools dashboard for Django projects
Stars: ✭ 266 (-28.88%)
Mutual labels:  django, django-admin
Django Wpadmin
WordPress look and feel for Django administration panel
Stars: ✭ 259 (-30.75%)
Mutual labels:  django, django-admin
Django Inline Actions
django-inline-actions adds actions to each row of the ModelAdmin or InlineModelAdmin.
Stars: ✭ 170 (-54.55%)
Mutual labels:  django, django-admin
Djangocms Admin Style
django CMS Admin Style is a Django Theme tailored to the needs of django CMS.
Stars: ✭ 282 (-24.6%)
Mutual labels:  django, django-admin
Django Admin Autocomplete Filter
A simple Django app to render list filters in django admin using autocomplete widget.
Stars: ✭ 166 (-55.61%)
Mutual labels:  django, django-admin
Django Suit
Modern theme for Django admin interface
Stars: ✭ 2,136 (+471.12%)
Mutual labels:  django, django-admin
Django Material Admin
Material design for django administration
Stars: ✭ 163 (-56.42%)
Mutual labels:  django, django-admin
Django Admin List Filter Dropdown
Use dropdowns in Django admin list filter
Stars: ✭ 215 (-42.51%)
Mutual labels:  django, django-admin
Django Subadmin
A special kind of ModelAdmin that allows it to be nested within another ModelAdmin
Stars: ✭ 120 (-67.91%)
Mutual labels:  django, django-admin
Django Business Logic
Visual DSL framework for django
Stars: ✭ 134 (-64.17%)
Mutual labels:  django, django-admin
Django Mptt Admin
Django-mptt-admin provides a nice Django Admin interface for Mptt models
Stars: ✭ 256 (-31.55%)
Mutual labels:  django, django-admin
Django Page Cms
Official Django page CMS git repository
Stars: ✭ 277 (-25.94%)
Mutual labels:  django, django-admin

Django Object Actions

Build Status

If you've ever tried making admin object tools you may have thought, "why can't this be as easy as making Django Admin Actions?" Well now they can be.

Quick-Start Guide

Install Django Object Actions:

$ pip install django-object-actions

Add django_object_actions to your INSTALLED_APPS so Django can find our templates.

In your admin.py:

from django_object_actions import DjangoObjectActions

class ArticleAdmin(DjangoObjectActions, admin.ModelAdmin):
    def publish_this(self, request, obj):
        publish_obj(obj)
    publish_this.label = "Publish"  # optional
    publish_this.short_description = "Submit this article"  # optional

    change_actions = ('publish_this', )

Usage

Defining new &tool actions is just like defining regular admin actions. The major difference is the functions for django-object-actions will take an object instance instead of a queryset (see Re-using Admin Actions below).

Tool actions are exposed by putting them in a change_actions attribute in your admin.ModelAdmin. You can also add tool actions to the main changelist views too. There, you'll get a queryset like a regular admin action:

from django_object_actions import DjangoObjectActions

class MyModelAdmin(DjangoObjectActions, admin.ModelAdmin):
    def toolfunc(self, request, obj):
        pass
    toolfunc.label = "This will be the label of the button"  # optional
    toolfunc.short_description = "This will be the tooltip of the button"  # optional

    def make_published(modeladmin, request, queryset):
        queryset.update(status='p')

    change_actions = ('toolfunc', )
    changelist_actions = ('make_published', )

Just like admin actions, you can send a message with self.message_user. Normally, you would do something to the object and return to the same url, but if you return a HttpResponse, it will follow it (hey, just like admin actions!).

If your admin modifies get_urls, change_view, or changelist_view, you'll need to take extra care because django-object-actions uses them too.

Re-using Admin Actions

If you would like a preexisting admin action to also be an object action, add the takes_instance_or_queryset decorator to convert object instances into a queryset and pass querysets:

from django_object_actions import DjangoObjectActions, takes_instance_or_queryset

class RobotAdmin(DjangoObjectActions, admin.ModelAdmin):
    # ... snip ...

    @takes_instance_or_queryset
    def tighten_lug_nuts(self, request, queryset):
        queryset.update(lugnuts=F('lugnuts') - 1)

    change_actions = ['tighten_lug_nuts']
    actions = ['tighten_lug_nuts']

Customizing Object Actions

To give the action some a helpful title tooltip, add a short_description attribute, similar to how admin actions work:

def increment_vote(self, request, obj):
    obj.votes = obj.votes + 1
    obj.save()
increment_vote.short_description = "Increment the vote count by one"

By default, Django Object Actions will guess what to label the button based on the name of the function. You can override this with a label attribute:

def increment_vote(self, request, obj):
    obj.votes = obj.votes + 1
    obj.save()
increment_vote.label = "Vote++"

If you need even more control, you can add arbitrary attributes to the buttons by adding a Django widget style attrs attribute:

def increment_vote(self, request, obj):
    obj.votes = obj.votes + 1
    obj.save()
increment_vote.attrs = {
    'class': 'addlink',
}

Programmatically Disabling Actions

You can programmatically disable registered actions by defining your own custom get_change_actions() method. In this example, certain actions only apply to certain object states (e.g. You should not be able to close an company account if the account is already closed):

def get_change_actions(self, request, object_id, form_url):
    actions = super(PollAdmin, self).get_change_actions(request, object_id, form_url)
    actions = list(actions)
    if not request.user.is_superuser:
        return []

    obj = self.model.objects.get(pk=object_id)
    if obj.question.endswith('?'):
        actions.remove('question_mark')

    return actions

The same is true for changelist actions with get_changelist_actions.

Alternate Installation

You don't have to add this to INSTALLED_APPS, all you need to to do is copy the template django_object_actions/change_form.html some place Django's template loader will find it.

If you don't intend to use the template customizations at all, don't add django_object_actions to your INSTALLED_APPS at all and use BaseDjangoObjectActions instead of DjangoObjectActions.

More Examples

Making an action that links off-site:

def external_link(self, request, obj):
    from django.http import HttpResponseRedirect
    return HttpResponseRedirect(f'https://example.com/{obj.id}')

Limitations

  1. django-object-actions expects functions to be methods of the model admin. While Django gives you a lot more options for their admin actions.
  2. If you provide your own custom change_form.html, you'll also need to manually copy in the relevant bits of our change form .
  3. Security. This has been written with the assumption that everyone in the Django admin belongs there. Permissions should be enforced in your own actions irregardless of what this provides. Better default security is planned for the future.

Python and Django compatibility

See tox.ini for which Python and Django versions this supports.

Demo Admin & Docker images

You can try the demo admin against several versions of Django with these Docker images: https://hub.docker.com/r/crccheck/django-object-actions/tags

This runs the example Django project in ./example_project based on the "polls" tutorial. admin.py demos what you can do with this app.

Development

Getting started (with virtualenvwrapper):

# get a copy of the code
git clone [email protected]:crccheck/django-object-actions.git
cd django-object-actions
# set up your virtualenv (with virtualenvwrapper)
mkvirtualenv django-object-actions
# Install requirements
make install
# Hack your path so that we can reference packages starting from the root
add2virtualenv .
make test  # run test suite
make quickstart  # runs 'make resetdb' and some extra steps

This will install whatever the latest stable version of Django is. You can also install a specific version of Django and pip install -r requirements.txt.

Various helpers are available as make commands. Type make help and view the Makefile to see what other things you can do.

Similar Packages

If you want an actions menu for each row of your changelist, check out Django Admin Row Actions.

Django Object Actions is very similar to django-object-tools, but does not require messing with your urls.py, does not do anything special with permissions, and uses the same patterns as making admin actions.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].