### Define Django Admin Cell Actions Source: https://www.noondot.com/docs/simplepro/config/admin/cell_action This Python code defines a Django model `CellActionModel` and its corresponding admin configuration `CellActionModelAdmin`. It demonstrates how to create custom actions (`test_action`, `test_action2`, `test_action3`) and associate them with table cells using `CellAction` and `CellMultipleAction` for single and multiple action displays in the Django admin interface. ```python class CellActionModel(models.Model): name = fields.CharField(max_length=32, verbose_name='名字') desc = fields.CharField(max_length=32, verbose_name='描述', null=True, blank=True) status = models.BooleanField(verbose_name='状态', default=False) def __str__(self): return self.name class Meta: verbose_name = '单元格Action' verbose_name_plural = '单元格Action' @admin.register(CellActionModel) class CellActionModelAdmin(admin.ModelAdmin, SourceCodeAdmin): list_display = ('id', 'name', 'status', 'custom_action', 'custom_multiple_action', 'custom_action_boolean') actions = ('test_action', 'test_action2', 'test_action3') def test_action(self, request, queryset): print(queryset) return JsonResponse(data={ 'status': 'success', 'msg': '处理成功!' }) test_action.confirm = "您确定要执行该操作吗?" def test_action2(self, request, queryset): print(queryset) return JsonResponse(data={ 'status': 'success', 'msg': '处理成功!' }) def test_action3(self, request, queryset): if queryset.count() == 1: obj = queryset.first() obj.status = not obj.status obj.save() return JsonResponse(data={ 'status': 'success', 'msg': '修改状态成功!' }) def custom_action(self, obj): return CellAction(text='调用', action=self.test_action) custom_action.short_description = '调用单个Action' def custom_multiple_action(self, obj): return CellMultipleAction(actions=[ CellAction(text='调用1', action=self.test_action), CellAction(text='调用2', action=self.test_action2) ]) custom_multiple_action.short_description = '调用多个Action' def custom_action_boolean(self, obj): html = '切换状态' if obj.status: html += ' ' else: html += '' return CellAction(text=html, action=self.test_action3) custom_action_boolean.short_description = '点击Action切换状态' ``` -------------------------------- ### Python CellAction Class Definition Source: https://www.noondot.com/docs/simplepro/config/admin/cell_action Defines the CellAction class for handling single-cell operations. It allows specifying display text and the action function to be called, supporting confirmation prompts. ```python class CellAction(BaseAction): """ 单个单元格的操作 """ def __init__(self, text, action): """ :param text: 显示的文本,支持普通文本、html和vue组件 :param action: 调用的函数, 传入参数为request,queryset(只包含当前行的数据),支持自定义按钮的confirm提示框 """ self.text = text self.action = action def to_dict(self): return { '_type': self.__class__.__name__, 'text': self.text, 'action': self.action.__name__ } ``` -------------------------------- ### Python Django Admin Integration with CellAction Source: https://www.noondot.com/docs/simplepro/config/admin/cell_action Demonstrates integrating CellAction within a Django admin model. It shows how to define a custom action method and return a CellAction object to be displayed in a table column. ```python class CellActionModel(models.Model): name = fields.CharField(max_length=32, verbose_name='名字') desc = fields.CharField(max_length=32, verbose_name='描述', null=True, blank=True) status = models.BooleanField(verbose_name='状态', default=False) # 用户 def __str__(self): return self.name class Meta: verbose_name = '单元格Action' verbose_name_plural = '单元格Action' @admin.register(CellActionModel) class CellActionModelAdmin(admin.ModelAdmin, SourceCodeAdmin): """ 单元格直接调用action """ # list_display = ("id", 'name', 'desc', 'status', 'custom_action', 'custom_multiple_action') list_display = ('id', 'name', 'status', 'custom_action',) # 指定单元格要调用的是哪个action actions = ('test_action') def test_action(self, request, queryset): print(queryset) return JsonResponse(data={ 'status': 'success', 'msg': '处理成功!' }) # 可以添加一个确认提示 test_action.confirm = "您确定要执行该操作吗?" def custom_action(self, obj): return CellAction(text='调用', action=self.test_action) custom_action.short_description = '调用单个Action' ``` -------------------------------- ### Python CellMultipleAction Class Definition Source: https://www.noondot.com/docs/simplepro/config/admin/cell_action Defines the CellMultipleAction class for managing operations across multiple cells. It accepts a tuple of actions to be performed. ```python class CellMultipleAction(BaseAction): """ 多个单元格的操作 """ def __init__(self, actions=()): self.actions = actions def to_dict(self): return { '_type': self.__class__.__name__, 'actions': [a.to_dict() for a in self.actions] } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.