Django:在 GenericStackedInline 上禁止 can_delete

发布于 2024-08-05 05:03:42 字数 950 浏览 4 评论 0原文

我已经构建了这个包含通用外键的模型:

class MyModel(models.Model):
    content_type = models.ForeignKey(ContentType, verbose_name=_('content type'))
    object_id = models.PositiveIntegerField(_('object id'))
    content_object = generic.GenericForeignKey('content_type', 'object_id')

接下来我制作了一个通用堆叠内联以将其放入任何 ModelAmin 类中:

class MyModelStackedInline(generic.GenericStackedInline):
    model = MyModel
    formset = generic.generic_inlineformset_factory(MyModel, can_delete=False)
    extra = 0

class SomeOhterModelAdmin(admin.ModelAdmin):
    inlines = [MyModelStackedInline]

但是,尽管在 generic_inlineformset_factory 中传递了 can_delete=False arg ,我总是在我的管理 Change_form 中看到一个 Delete 复选框。

这是一个示例:http://img8.imageshack.us/img8/3323/screenshotbe。 png

你知道如何删除这个复选框吗?

谢谢 :)

I've built this model which contains a generic foreign key:

class MyModel(models.Model):
    content_type = models.ForeignKey(ContentType, verbose_name=_('content type'))
    object_id = models.PositiveIntegerField(_('object id'))
    content_object = generic.GenericForeignKey('content_type', 'object_id')

Next I've made a generic stacked inline to put it in any ModelAmin class:

class MyModelStackedInline(generic.GenericStackedInline):
    model = MyModel
    formset = generic.generic_inlineformset_factory(MyModel, can_delete=False)
    extra = 0

class SomeOhterModelAdmin(admin.ModelAdmin):
    inlines = [MyModelStackedInline]

However, despite the can_delete=False arg passed by in generic_inlineformset_factory, I always see a Delete checkbox in my admin change_form.

Here is an example: http://img8.imageshack.us/img8/3323/screenshotbe.png

Do you know how to remove this checkbox ?

Thank you :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尾戒 2024-08-12 05:03:42

也许这是 09 年后的功能,但您可以指定它,而无需覆盖 __init__() 方法:

class StupidCarOptionsInline(admin.StackedInline):
    model = models.StupidOption
    form = StupidCarOptionAdminForm
    extra = 0
    can_delete = False

Maybe It is a post '09 feature, but you can specify that without overriding the __init__() method :

class StupidCarOptionsInline(admin.StackedInline):
    model = models.StupidOption
    form = StupidCarOptionAdminForm
    extra = 0
    can_delete = False
时光与爱终年不遇 2024-08-12 05:03:42

2016 年更新:根据下面 Stan 的回答,现代版本的 django 允许您设置 can_delete = TrueGenericStackedInline 子类,因为它继承自 InlineModelAdmin


我以前遇到过这个 - 由于某种原因通过can_delete 作为参数不起作用,但在表单集的 init 方法中设置它可以。试试这个:

class MyInlineFormset(generic.generic_inlineformset_factory(MyModel)):
    def __init__(self, *args, **kwargs):
        super(MyInlineFormset, self).__init__(*args, **kwargs)
        self.can_delete = False

然后在你的管理内联类中:

class MyModelStackedInline(generic.GenericStackedInline):
    model = MyModel
    formset = MyInlineFormset
    extra = 0

Update 2016: as per Stan's answer below, modern versions of django let you set can_delete = True on the GenericStackedInline subclass, as it inherits from InlineModelAdmin


I've run into this before - for some reason passing can_delete as an argument doesn't work, but setting it in the formset's init method does. Try this:

class MyInlineFormset(generic.generic_inlineformset_factory(MyModel)):
    def __init__(self, *args, **kwargs):
        super(MyInlineFormset, self).__init__(*args, **kwargs)
        self.can_delete = False

then in your admin inline class:

class MyModelStackedInline(generic.GenericStackedInline):
    model = MyModel
    formset = MyInlineFormset
    extra = 0
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文