1👍
✅
From my experience, you can’t add new fields after super().init(). and fields doesn’t exist before the _init. So the work around to always include the optional fields and then remove after init if they are not needed.
class JoinForm(forms.ModelForm):
captcha = NoReCaptchaField(gtag_attrs={'data-theme': 'light'})
class Meta:
model = InterestedUser
fields = ('email', 'name', 'subject', 'via', 'content',)
def __init__(self, needs_captcha, *args, **kwargs):
super().__init__(*args, **kwargs)
if not needs_captcha:
del self.fields['captcha']
Source:stackexchange.com