Django Model instance value in ModelForm constructor -
i create dynamic form
based on modelform
. aim add fields information in json
field.
class myform(forms.modelform): class meta: model = mymodel fields = ['name', 'json'] def __init__(self, *args, **kwargs): super(myform, self).__init__(*args, **kwargs) [ create fields here]
i can create fields dynamically this:
variables = ('var_1', 'var_2',) v in variables: self.fields[v] = forms.charfield(label=v)
now, replace variables
json.variables
value. tried this: self.fields['json'].initial
, self.fields['json'].data
, self.fields['json'].cleaned_data
without success.
do know how can have access model value?
finally solution quite easy. have use self.instance.json
.
Comments
Post a Comment