[Answered ]-Extending Django Admin tabular.html custom checkbox action

2👍

Suppose you have to models:

class Hive(models.Model):
    name = models.CharField(max_length=100)
    marker = models.BooleanField(default=False)


class Bee(models.Model):
    hive = models.ForeignKey(Hive)
    name = models.CharField(max_length=100)

then your admin could look like this:

class BeeForm(forms.ModelForm):
    check = forms.BooleanField(label="check", required=False)
    class Meta:
        model = Bee

class BeeInline(admin.TabularInline):
    form = BeeForm
    model = Bee

class HiveAdmin(admin.ModelAdmin):
    inlines = (BeeInline,)
    def save_formset(self, request, form, formset, change):
        for bee_form in formset:
            if bee_form.cleaned_data.get('check') == True:
                form.instance.marker = True
                form.instance.save()
        super(HiveAdmin, self).save_formset(request, form, formset, change)

admin.site.register(Hive, HiveAdmin)

Leave a comment