I want to exclude, programatically, a field in my form. Currently I have this:
class RandomForm(BaseForm):
def __init__(self, *args, **kwargs):
# This doesn't work
if kwargs["instance"] is None:
self._meta.exclude = ("active",)
super(ServiceForm, self).__init__(*args, **kwargs)
# This doesn't work either
if kwargs["instance"] is None:
self._meta.exclude = ("active",)
class Meta:
model = models.Service
fields = (...some fields...)
How can I exclude the active
field only when a new model is being created?
You can solve it this way:
class RandomForm(ModelForm):
def __init__(self, *args, **kwargs):
super(RandomForm, self).__init__(*args, **kwargs)
if not self.instance:
self.fields.pop('active')
class Meta:
model = models.Service
fields = (...some fields...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With