0
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

3
  • I'm not sure what you mean my add that field to that assignment. Please can you clarify this a bit better? Commented Feb 7, 2013 at 12:21
  • 1
    Your question is not clarify. Can you put your model codes Commented Feb 7, 2013 at 13:03
  • What do you expect after m.save()? Do you expect that django creates new field is_ok in table for Example model in database? Commented Feb 7, 2013 at 14:05

1 Answer 1

1

You really should do this in the declaration of your ExampleForm class. I assume the class looks something like this

from your_app.models import ExampleModel
from django.forms import ModelForm

class ExampleForm(ModelForm):
    class Meta:
        model = ExampleModel

You just need to declare the field on the form. Something like -

class ExampleForm(ModelForm)
    is_ok = forms.BooleanField()
    class Meta:
        model = ExampleModel

The form class will now handle the creation of the input element for the is_ok field, 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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.