Last active
April 1, 2020 09:27
-
-
Save daimon99/0bd170deb1c41d2107ee34c3267d8f09 to your computer and use it in GitHub Desktop.
Django admin action with intermedia page.Django admin 的 action 使用中间页
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @admin.register(WBS) | |
| class WBSAdmin(admin.ModelAdmin): | |
| list_display = ( | |
| 'id', | |
| 'code', | |
| 'name', | |
| 'parent', | |
| 'pv', | |
| 'ev', | |
| 'created', | |
| 'modified', | |
| ) | |
| list_filter = ('created', 'modified', 'parent') | |
| search_fields = ('name',) | |
| list_editable = ('parent', 'pv', 'ev') | |
| ordering = ('-id',) | |
| actions = ['do_batch_update_code'] | |
| def do_batch_update_parent(self, request, qs): | |
| """批量更新父任务""" | |
| if 'apply' in request.POST: | |
| parent_id = request.POST.get('parent_id') | |
| qs.update(parent_id=parent_id) | |
| self.message_user(request, f'选择的任务的父级任务已经设置为:{parent_id}') | |
| return HttpResponseRedirect(reverse('admin:djpmp_wbs_changelist')) | |
| context = { | |
| 'title': '选择父级任务', | |
| 'tasks': m.WBS.objects.all(), | |
| 'task_selected': qs | |
| } | |
| return render(request, 'admin/djpmp/wbs/batch-update-parent.html', context=context) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {% extends 'admin/base_site.html' %} | |
| {% load i18n admin_urls static admin_list %} | |
| {% load mptt_tags %} | |
| {% block content %} | |
| <form action="" method="POST"> | |
| {% csrf_token %} | |
| <div> | |
| <fieldset class="module aligned"> | |
| <div class="form-row"> | |
| <div> | |
| <label> | |
| <h3>以下任务:</h3> | |
| </label> | |
| <ul style="list-style-type: none;"> | |
| {% for task in task_selected %} | |
| <li style=""> | |
| {{ task.name }} | |
| <input type="hidden" name="_selected_action" value="{{ task.pk }}"/> | |
| </li> | |
| {% endfor %} | |
| </ul> | |
| </div> | |
| </div> | |
| <div class="form-row"> | |
| <div> | |
| <label class="required" for="parent_id"> | |
| <h3>父级任务设置为:</h3> | |
| </label> | |
| <ul style="list-style-type: none;"> | |
| {% recursetree tasks %} | |
| <li style="list-style-type: none"> | |
| <label> | |
| <input type="radio" id="parent_id" name="parent_id" value="{{ node.pk }}"> | |
| {{ node.name }} | |
| </label> | |
| {% if not node.is_leaf_node %} | |
| <ul class="children" style="list-style-type: none;"> | |
| {{ children }} | |
| </ul> | |
| {% endif %} | |
| </li> | |
| {% endrecursetree %} | |
| </ul> | |
| </div> | |
| </div> | |
| </fieldset> | |
| <div class="submit-row"> | |
| <input type="hidden" name="action" value="do_batch_update_parent"/> | |
| <input type="submit" name="apply" value="提交"/>   | |
| <button type="button" name="cancel" onclick="history.back(-1);" class="module">取消</button> | |
| </div> | |
| </div> | |
| </form> | |
| {% endblock %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment