change model admin form default value

django, django-admin, python

Solution

As you suggest, I think the easiest approach is to change the model form used for the `B` model in the django admin.

To change the initial value of the form field, you can either redefine the field, or override the `__init__` method.

class BForm(forms.ModelForm):
    # either redefine the boolean field
    boolean_field = models.BooleanField(initial=False)

    class Meta:
        model = B

    # or override the __init__ method and set initial=False
    # this is a bit more complicated but less repetitive
    def __init__(self, *args, **kwargs):
        super(BForm, self).__init__(*args, **kwargs)
        self.fields['boolean_field'].initial = False

Using your custom model form in the django admin is easy!

class BAdmin(admin.ModelAdmin):
    form = BForm

admin.site.register(B, BAdmin)

Problem

I have typical model inheritance in my project: ``` class A(models.Model): boolean_field = models.BooleanField(default=True) class B(A): some_other_field = models.CharField() ``` I want to override default value of `boolean_field` in class `B`, how can I do it? I think that could be tricky thing to do on a database layer, so maybe at least I can simply override that default value in Django admin (I mean in `ModelAdmin` form for class `B`).

Original source

Related problems