如何从TabularInline管理字段中删除添加和更改按钮? [英] How can I remove the add and change buttons from a TabularInline admin field?

查看:231
本文介绍了如何从TabularInline管理字段中删除添加和更改按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有模型 A B AB
A 对象有一个 ManyToManyField 称为 Am 可以通过中介模型 AB 链接到许多 B 对象。

我有一个在我的 A的管理页面上,非常漂亮的 TabularInline 部分充满了 AB 个对象模型。

I have models A, B, and AB. A objects have a ManyToManyField called A.m that can link to many B objects, through my intermediary model AB.
I have a very nice TabularInline section full of AB objects, on my admin page for my A model.

一切都很好。除了 TabularInline 部分为每个<$ c $中的 B 对象显示添加和更改按钮外, c> AB 对象的行,我想删除这些按钮。我仍然希望能够添加,更改和删除 AB 对象行,而不是它们引用的 B 对象。

All is well. Except that the TabularInline section shows "Add" and "Change" buttons for the B object in each AB object's row, and I want to remove those buttons. I still want to be able to add, change, and delete AB objects rows, just not the B objects they reference.

我尝试设置 can_add_related can_change_related ,将 can_delete_related 属性设置为 False ,但这无济于事。

I have tried setting the can_add_related, can_change_related, can_delete_related attributes to False, but this does nothing.

class ABInline(admin.TabularInline):
    model = AB
    def get_form(self, request, obj=None, **kwargs):
        form = super(ABInline, self).get_form(request, obj, **kwargs)
        form.base_fields['m'].widget.can_add_related = False
        form.base_fields['m'].widget.can_change_related = False
        form.base_fields['m'].widget.can_delete_related = False
        return form

这是一个错误吗?还是对于 TabularInline 字段有另一种实现方法?

Is this a bug? Or is there a different way to accomplish this for TabularInline fields?

推荐答案

OP设置小部件属性的想法应该起作用。

The OP's idea of setting the widget's attributes should work.

基本思想如下:

TabularInline中的实际表单字段 AB 允许您选择 B 对象是 ModelChoiceField 。此字段具有选择小部件,包装在< a href = https://github.com/django/django/blob/2.1.7/django/contrib/admin/widgets.py#L234 rel = nofollow noreferrer> RelatedFieldWidgetWrapper 。后者控制选择框旁边的添加和更改(或编辑)按钮。要删除这些按钮,请将小部件的 can_add_related can_change_related 属性设置为 False

The actual form field in the TabularInline for AB that allows you to select the B object is a ModelChoiceField. This field has a Select widget, wrapped in a RelatedFieldWidgetWrapper. The latter controls the "add" and "change" (or "edit") buttons next to the select box. To remove these buttons, set the widget's can_add_related and can_change_related attributes to False.

这实际上是OP尝试执行的操作。但是,OP尝试扩展 get_form ,但是该方法仅在 ModelAdmin 上可用,而在 TabularInline ,据我所知()。

This is actually what the OP attempted to do. However, the OP tried to extend get_form, but that method is only available on a ModelAdmin, not on the TabularInline, as far as I know (source).

我们可以扩展例如,代替使用 get_form formfield_for_dbfield )放在 TabularInline 上,如下所示(基于OP的示例):

Instead of using get_form, we can extend e.g. formfield_for_dbfield (source) on the TabularInline, as illustrated below (based on OP's example):

class ABInline(admin.TabularInline):
    model = AB

    def formfield_for_dbfield(self, db_field, request, **kwargs):
        formfield = super(ABInline, self).formfield_for_dbfield(
            db_field, request, **kwargs)
        if db_field.name == 'b':
            # Assuming AB.b is the ForeignKey to B
            formfield.widget.can_add_related = False
            formfield.widget.can_change_related = False
            # formfield.widget.can_delete_related = False  # default is already False
        return formfield

这里我们假设OP的 AB 模型看起来像这样:

Here we assume that the OP's AB model looks something like this:

class AB(models.Model):
    a = models.ForeignKey(to='A', ...)
    b = models.ForeignKey(to='B', ...)
    ...

这篇关于如何从TabularInline管理字段中删除添加和更改按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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