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
Source:stackexchange.com