【问题标题】:"Save as" and "Save and add another" in Admin管理员中的“另存为”和“保存并添加另一个”
【发布时间】:2011-01-13 14:22:33
【问题描述】:

有没有办法在 django 管理站点中同时“另存为”和“保存并添加另一个”?

【问题讨论】:

  • 你会如何另存为?每个表单要么保存一个新对象并返回到列表(保存),要么保存一个新对象并返回到空白的新对象窗体(保存并添加另一个),或者保存并保留在该对象的编辑页面上(保存并继续编辑)。
  • 如果你把 save_as = True 在你的 admin.py 你得到另存为按钮。但这所做的是替换保存并通过另存为添加另一个按钮。但我希望能够有两个选择。

标签: django


【解决方案1】:

我不认为按钮引用的 URL 有任何魔力,因此您可以通过简单地覆盖每个 http://docs.djangoproject.com/en/dev/ref/contrib/admin/#overriding-admin-templates 的管理模板来添加另一个缺少功能的按钮

【讨论】:

    【解决方案2】:

    我设法通过覆盖 admin_modify.py 中的默认行为解决了这个问题(this 这篇文章帮助了我,但实际上并没有为我工作)

    这是对 django 1.6 原始源代码的修改。放入/app/templatetags/admin_modify.py(别忘了导入/app/templatetags/__init__.py

    from django.contrib.admin.templatetags import admin_modify
    
    @admin_modify.register.inclusion_tag('admin/submit_line.html', takes_context=True)
    def submit_row(context):
        opts = context['opts']
        change = context['change']
        is_popup = context['is_popup']
        save_as = context['save_as']
        ctx = {
            'opts': opts,
            'show_delete_link': (not is_popup and context['has_delete_permission']
                                  and change and context.get('show_delete', True)),
            'show_save_as_new': not is_popup and change and save_as,
            'show_save_and_add_another': context['has_add_permission'] and
                                not is_popup,
            'show_save_and_continue': not is_popup and context['has_change_permission'],
            'is_popup': is_popup,
            'show_save': True,
            'preserved_filters': context.get('preserved_filters'),
        }
        if context.get('original') is not None:
            ctx['original'] = context['original']
        return ctx
    
    admin_modify.submit_row = submit_row
    

    源代码有:

    'show_save_and_add_another': context['has_add_permission'] and
                  not is_popup and (not save_as or context['add']),
    

    【讨论】:

    • 我也必须更改文件 .../django/contrib/admin/templates/admin/submit_line.html(查看按钮)并在 / 中输入“from admin_modify import *” /templatetags/__init__.py 它应该可以工作还是我错过了什么?
    猜你喜欢
    • 2012-10-17
    • 2019-01-24
    • 2014-11-18
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多