如何在Django管理员上添加只读内联 [英] How to add readonly inline on django admin

查看:58
本文介绍了如何在Django管理员上添加只读内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用django 1.4,并且有一个many2many字段,因此在创建管理站点时,我想将此字段作为内联添加,这是一些代码:

I am using django 1.4 and I have a many2many field, so when creating the admin site I wanted to add this field as an inline, here is some code:

class SummaryInline(admin.TabularInline):
    model = ParserError.summaries.through


class MyClassAdmin(admin.ModelAdmin):
    list_display = ('classifier', 'name', 'err_count', 'supported')
    fields = ('classifier', 'name', 'err_count', 'err_classifier', 'supported')
    inlines = (SummaryInline,)
    readonly_fields = ('classifier', 'err_count')

所以我的问题是,如何使内联字段变为只读状态?

So my question is, how can I make the inline field readonly?

推荐答案

一段时间后,我尝试通过 readonly_fields ,设置 can_delete 设置为False.

After a while of trying to find the name I figured out thanks to this answer, so I checked the names at self.opts.local_fields and found the name of the middle table and added it to readonly_fields, setting can_delete to False.

class SummaryInline(admin.TabularInline):
    model = ParserError.summaries.through
    readonly_fields = ('myclasssummary',)
    can_delete = False

很简单,但是花了我一段时间,所以我发现在这里添加它是个好主意.

pretty simple but took me a while so I figured out it was a good idea to add it here.

这篇关于如何在Django管理员上添加只读内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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