[Django]-Dynamically adding new fields to a form in Django

4👍

You can add them after calling super.

class GuideForm(BootstrapModelForm):
    class Meta:
        model = Guide
        exclude = ('author', 'created', 'modified', 'hero', 'dust', 'rating',)

    def __init__(self, *args, **kwargs):
        hero = kwargs.pop('hero', None)
        super(GuideForm, self).__init__(*args, **kwargs)

        #  add your fields here
        self.fields['title_1'] = forms.CharField()

You can check out this good example https://stackoverflow.com/a/6142749/2698552

Leave a comment