views:

118

answers:

1

In django admin, there are fields I'd like to require if a model is being edited standalone. If it is inline, I don't want them to be required. Is there a way to do this?

+3  A: 

Sure. Just define a custom form, with your required field overridden to set required=True, and use it in your admin class.

class MyForm(forms.ModelForm):
    required_field = forms.CharField(required=True)

    class Meta:
        model = MyModel

class MyAdmin(admin.ModelAdmin):
    form = MyForm


class MyInlineAdmin(admin.ModelAdmin):
    model = MyModel

So here MyAdmin is using the overridden form, but MyInlineAdmin isn't.

Daniel Roseman
Thanks very much
Mitch