@@ -22,13 +22,12 @@ our templates.
2222In your admin.py:
2323
2424``` python
25- from django_object_actions import DjangoObjectActions
25+ from django_object_actions import DjangoObjectActions, action
2626
2727class ArticleAdmin (DjangoObjectActions , admin .ModelAdmin ):
28+ @action (label = " Publish" , description = " Submit this article" ) # optional
2829 def publish_this (self , request , obj ):
2930 publish_obj(obj)
30- publish_this.label = " Publish" # optional
31- publish_this.short_description = " Submit this article" # optional
3231
3332 change_actions = (' publish_this' , )
3433```
@@ -49,10 +48,12 @@ views too. There, you'll get a queryset like a regular [admin action][admin acti
4948from django_object_actions import DjangoObjectActions
5049
5150class MyModelAdmin (DjangoObjectActions , admin .ModelAdmin ):
51+ @action (
52+ label = " This will be the label of the button" , # optional
53+ description = " This will be the tooltip of the button" # optional
54+ )
5255 def toolfunc (self , request , obj ):
5356 pass
54- toolfunc.label = " This will be the label of the button" # optional
55- toolfunc.short_description = " This will be the tooltip of the button" # optional
5657
5758 def make_published (modeladmin , request , queryset ):
5859 queryset.update(status = ' p' )
@@ -93,8 +94,18 @@ class RobotAdmin(DjangoObjectActions, admin.ModelAdmin):
9394
9495### Customizing * Object Actions*
9596
96- To give the action some a helpful title tooltip, add a
97- ` short_description ` attribute, similar to how admin actions work:
97+ To give the action some a helpful title tooltip, you can use the ` action ` decorator
98+ and set the description argument.
99+
100+ ``` python
101+ @action (description = " Increment the vote count by one" )
102+ def increment_vote (self , request , obj ):
103+ obj.votes = obj.votes + 1
104+ obj.save()
105+ ```
106+
107+ Alternatively, you can also add a ` short_description ` attribute,
108+ similar to how admin actions work:
98109
99110``` python
100111def increment_vote (self , request , obj ):
@@ -107,6 +118,15 @@ By default, Django Object Actions will guess what to label the button
107118based on the name of the function. You can override this with a ` label `
108119attribute:
109120
121+ ``` python
122+ @action (label = " Vote++" )
123+ def increment_vote (self , request , obj ):
124+ obj.votes = obj.votes + 1
125+ obj.save()
126+ ```
127+
128+ or
129+
110130``` python
111131def increment_vote (self , request , obj ):
112132 obj.votes = obj.votes + 1
@@ -119,6 +139,15 @@ by adding a Django widget style
119139[ attrs] ( https://docs.djangoproject.com/en/stable/ref/forms/widgets/#django.forms.Widget.attrs )
120140attribute:
121141
142+ ``` python
143+ @action (attrs = {' class' : ' addlink' })
144+ def increment_vote (self , request , obj ):
145+ obj.votes = obj.votes + 1
146+ obj.save()
147+ ```
148+
149+ or
150+
122151``` python
123152def increment_vote (self , request , obj ):
124153 obj.votes = obj.votes + 1
0 commit comments