Reputation: 3491
I have a model which is registered with the admin.
models.py
class Post(models.Model):
title = models.CharField(max_length=100)
tag = models.CharField(max_length=3)
is_enabled = models.BooleanField(default=False)
Now, I want that the admin can only enable or disable the Post by interacting with is_enabled field of the model.
admin.py
class PostAdmin(admin.ModelAdmin):
list_display = ['id', 'title', 'tag', 'is_enabled']
list_display_links = None
readonly_fields = ['id', 'title', 'tag']
actions = ['enable_selected', 'disable_selected']
def enable_selected(self,requst,queryset):
queryset.update(is_enabled=True)
def disable_selected(self,requst,queryset):
queryset.update(is_enabled=False)
enable_selected.short_description = "Enable the selected Post"
disable_selected.short_description = "Disable the selected Post"
I have successfully added these actions on the dropdown, however I need to add this in the form of a button on the list, also I need to know how can I call a function when the button is hit to update the is_enabled field.
Upvotes: 0
Views: 1377
Reputation: 576
You can use list_editable
list_editable = ['is_enabled']
Remember to add is_enabled in list_display (ModelAdmin)also
Upvotes: 2