Django: disallow can_delete on GenericStackedInline
Asked Answered
M

2

23

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 :)

Mosley answered 24/9, 2009 at 10:16 Comment(0)
A
14

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
Attention answered 24/9, 2009 at 20:48 Comment(1)
Sorry for answering so (very) late. It's now working. I will probably open a new ticket on django website about it. Thank you. ;-)Mosley
C
45

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
Cognize answered 4/2, 2011 at 8:33 Comment(2)
"can_delete = False" was just what I was looking for.Anam
This is the best anwerNitriding
A
14

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
Attention answered 24/9, 2009 at 20:48 Comment(1)
Sorry for answering so (very) late. It's now working. I will probably open a new ticket on django website about it. Thank you. ;-)Mosley

© 2022 - 2024 — McMap. All rights reserved.