Django管理界面中的只读模型?

如何在管理界面中将模型完全设为只读?它用于一种日志表,我在其中使用管理功能来搜索,排序,过滤等,但无需修改日志。

万一这看起来像是重复的,这不是我想要做的:

  • 我不是在寻找只读字段(即使将每个字段都设为只读也可以让你创建新记录)
  • 我不是要创建一个只读用户:每个用户都应该是只读的。

回答:

class ReadOnlyAdminMixin(object):

"""Disables all editing capabilities."""

change_form_template = "admin/view.html"

def __init__(self, *args, **kwargs):

super(ReadOnlyAdminMixin, self).__init__(*args, **kwargs)

self.readonly_fields = self.model._meta.get_all_field_names()

def get_actions(self, request):

actions = super(ReadOnlyAdminMixin, self).get_actions(request)

del_action = "delete_selected"

if del_action in actions:

del actions[del_action]

return actions

def has_add_permission(self, request):

return False

def has_delete_permission(self, request, obj=None):

return False

def save_model(self, request, obj, form, change):

pass

def delete_model(self, request, obj):

pass

def save_related(self, request, form, formsets, change):

pass

templates / admin / view.html

{% extends "admin/change_form.html" %}

{% load i18n %}

{% block submit_buttons_bottom %}

<div class="submit-row">

<a href="../">{% blocktrans %}Back to list{% endblocktrans %}</a>

</div>

{% endblock %}

templates / admin / view.html(用于Grappelli)

{% extends "admin/change_form.html" %}

{% load i18n %}

{% block submit_buttons_bottom %}

<footer class="grp-module grp-submit-row grp-fixed-footer">

<header style="display:none"><h1>{% trans "submit options"|capfirst context "heading" %}</h1></header>

<ul>

<li><a href="../" class="grp-button grp-default">{% blocktrans %}Back to list{% endblocktrans %}</a></li>

</ul>

</footer>

{% endblock %}

以上是 Django管理界面中的只读模型? 的全部内容, 来源链接: utcz.com/qa/403578.html

回到顶部