Django 在 TabularInline 视图中禁用编辑(但允许添加)
我想禁用编辑特定 TabularInline 实例中的所有对象,同时仍然允许添加并仍然允许编辑父模型。
我有一个简单的设置:
class SuperviseeAdmin(admin.TabularInline):
model = Supervisee
class SupervisorAdmin(admin.ModelAdmin):
inlines = [SuperviseeAdmin]
admin.site.register(Supervisor, SupervisorAdmin)
我尝试向 SuperviseAdmin
添加一个 has_change_permission
函数,该函数无条件返回 False
,但没有任何效果。
我尝试在 SuperviseAdmin
中设置 actions = None
但没有效果。
我可能会忽略什么才能让它发挥作用?
I want to disable editing ALL objects within a particular TabularInline instance, while still allowing additions and while still allowing editing of the parent model.
I have this trivial setup:
class SuperviseeAdmin(admin.TabularInline):
model = Supervisee
class SupervisorAdmin(admin.ModelAdmin):
inlines = [SuperviseeAdmin]
admin.site.register(Supervisor, SupervisorAdmin)
I have tried adding a has_change_permission
function to SuperviseeAdmin
that returns False
unconditionally, but it had no effect.
I have tried setting actions = None
in SuperviseeAdmin
but it had no effect.
What might I be overlooking that could get this to work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用户 django admin 内置函数 has_change_permission() 并返回 false 以限制对象编辑视图。
User django admin build in function has_change_permission() and return false to restrict object Edit view.
您可以尝试创建一个单独的内联类(请参阅
InlineModelAdmin
文档),使用自定义ModelForm
,您可以在其中自定义clean
方法以在尝试更新时引发错误:You can try creating a separate inline class (see the
InlineModelAdmin
docs) that uses a customModelForm
where you can customise the theclean
method to throw an error when trying to update:请参阅此解决方案: Django admin:在 add 中使字段可编辑但不编辑
重写 get_readonly_fields 方法:
See this solution: Django admin: make field editable in add but not edit
Override get_readonly_fields method:
只需将 admin.TabularInline 中的所有字段设置为 readonly_fields 即可:
just make all your fields readonly_fields in your admin.TabularInline as :