[Answered ]-How to edit a form field to add some condition using Django template

1πŸ‘

βœ…

In your ExpenseForm, you can modify the attributes of the widget based on some arbitrary criteria using __init__.

For example, say you pass an entry object in your form, and you want to modify a widget based on that:

class ExpenseForm(ModelForm):
    ...
    def __init__(self, *args, **kwargs):
        entry = kwargs.pop('entry')
        super().__init__(*args, **kwargs)
        if entry:
            self.fields['project'].widget.attrs.update({'value': entry.project})
πŸ‘€Brian Destura

Leave a comment