如何使Django模型只在Django Admin中查看(只读)? [英] How to make Django model just view (read-only) in Django Admin?

查看:264
本文介绍了如何使Django模型只在Django Admin中查看(只读)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题是继续如何删除Django管理员中的添加按钮,特定模型?

我已经意识到我的第一个问题不是很好,所以我很坚强,最好是开始新的问题,那就是修复旧的问题。因为已经有一些答案。

I have realized that my first question was not formulated good, so I tough that it is better that I start new question, that to fix the old one. Because there was already some answers.

所以问题是如何使Django模型将被视为只读。所以你不能添加新的,删除旧的,改变当前的,但是你也没有在网页管理界面上的按钮。

So question is how to make Django Model that will be view read only. So that you can not add new, delete old, change current, but that you also do not have button for that on web admin UI.

第一个问题的解决方案都与字段相关,但不是整个模型。

它们都工作,意义上你将无法编辑这些字段,但我不满意他们如何做。

当前解决方案是:

Solution from first question are all related to fields, but not to whole model.
They all work, in sense that you will not be able to edit those field, but I am not satisfied how they do it.
Current solution are:


  1. 使用 readonly_fields 在模型的所有字段上 - >我不喜欢它,因为您可以单击行来更改它,但不能编辑字段。

  2. editable = False 在归档定义 - >这不会显示Web管理UI上的字段。但您仍然可以点击行,但您将看不到任何内容,并且仍然保存按钮。

  3. def has_add_permission(self,request): - >与2相同

  4. 不要给任何人这个模型的添加权限 - >相同2

  1. Use readonly_fields on all fields from model -> I do not like it because you can click on row to change it, but you can not edit fields.
  2. editable=False on filed definition -> This will not show the field on web admin UI. But you can still click on row, but you will just not see anything, and still have Save button.
  3. def has_add_permission(self, request): -> same as 2
  4. don't give anyone the add permission for this model -> same as 2

任何想法?

推荐答案

您需要将ModelAdmin类的 list_display_links 属性设置为(无,)。但是,只有在标准ModelAdmin __ init __ 调用后才可以在 __ init __ 中完成,否则将抛出不正确的配置异常与文本 ... list_display_links [0]'是指在'list_display'中未定义的无。而且您应该定义 has_add_permisssion ,以隐藏添加按钮:

You need to set list_display_links attribute of your ModelAdmin class to (None,). But this can be done only in __init__ after standard ModelAdmin __init__ call otherwise it will throw ImproperlyConfigured exception with text ... list_display_links[0]' refers to 'None' which is not defined in 'list_display'. And you should define has_add_permisssion anyway to hide add button:

class AmountOfBooksAdmin(admin.ModelAdmin):
    actions = None # disables actions dropbox with delete action
    list_display = ('book', 'amount')

    def has_add_permission(self, request):
        return False

    def __init__(self, *args, **kwargs):
        super(AmountOfBooksAdmin, self).__init__(*args, **kwargs)
        self.list_display_links = (None,)

    # to hide change and add buttons on main page:
    def get_model_perms(self, request): 
        return {'view': True}

要从madin管理页面隐藏查看和更改按钮,您必须放置索引.html从django / contrib / admin / templates / admin /到你的模板dir / admin并更改它:

To hide 'view' and 'change' buttons from madin admin page you must place index.html from django/contrib/admin/templates/admin/ to you templates dir /admin and change it:

    {% for model in app.models %}   
        ...

        {% if model.perms.view %}       
            <th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
        {% else %}
            {% if model.admin_url %}
                <th scope="row"><a href="{{ model.admin_url }}">{{ model.name }}</a></th>
            {% else %}
                <th scope="row">{{ model.name }}</th>
            {% endif %}
        {% endif %}

        ....

这篇关于如何使Django模型只在Django Admin中查看(只读)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆