Django:在 GenericStackedInline 上禁止 can_delete
我已经构建了这个包含通用外键的模型:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许这是 09 年后的功能,但您可以指定它,而无需覆盖
__init__()
方法:Maybe It is a post '09 feature, but you can specify that without overriding the
__init__()
method :2016 年更新:根据下面 Stan 的回答,现代版本的 django 允许您设置
can_delete = True
在GenericStackedInline
子类,因为它继承自InlineModelAdmin
我以前遇到过这个 - 由于某种原因通过can_delete 作为参数不起作用,但在表单集的 init 方法中设置它可以。试试这个:
然后在你的管理内联类中:
Update 2016: as per Stan's answer below, modern versions of django let you set
can_delete = True
on theGenericStackedInline
subclass, as it inherits fromInlineModelAdmin
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:
then in your admin inline class: