if request.method == 'POST':
try:
form = ExampleForm(request.POST,
instance=Example.objects.get(user=request.user))
except:
form = ExampleForm(request.POST)
if form.is_valid():
m = form.save(commit=False)
is_ok = request.POST.get('is_ok')
m.is_ok = is_ok
m.user = request.user
m.save()
How to add field is_ok to instance=Example.objects.get(user=request.user)
Note: I can not add this field to the model forms.
If I change existing data, my is_ok value changes to default
EDIT:
<input type="checkbox" disabled="disabled" checked="checked" name="is_ok" id="id_is_ok"/>
If this input is disabled, my is_ok = request.POST.get('is_ok') = None
You really should do this in the declaration of your ExampleForm class. I assume the class looks something like this
You just need to declare the field on the form. Something like –
The form class will now handle the creation of the
inputelement for theis_okfield, just as it does for all the other fields, but it will override the settings on the Model – it won’t be disabled any more (see the docs for more info).