I have faced this issue while creating a form using Django.
class DetailForm(forms.Form):
row_id = 2
def __init__(self, *args, **kwargs):
global row_id
row_id = kwargs.pop('r_id')
super(DetailForm, self).__init__(*args, **kwargs)
row=Prewash.objects.get(id=row_id)
id = forms.CharField(label='ID',initial=row_id, widget=forms.TextInput(attrs={'readonly':'readonly'}))
Here within the init() the global variable row_id is being updated. But it does not get reflected in the last line when I try to retrieve the value from the db.
I need a way to retain the value of the variable I am trying to set within the constructor(without using instance or methods).