python - How to add own form field to instance from models form? -



python - How to add own form field to instance from models form? -

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 add together field is_ok instance=example.objects.get(user=request.user)

note: can not add together field model forms.

if alter existing data, is_ok value changes default

edit:

<input type="checkbox" disabled="disabled" checked="checked" name="is_ok" id="id_is_ok"/>

if input disabled, is_ok = request.post.get('is_ok') = none

you should in declaration of exampleform class. assume class looks

from your_app.models import examplemodel django.forms import modelform class exampleform(modelform): class meta: model = examplemodel

you need declare field on form. -

class exampleform(modelform) is_ok = forms.booleanfield() class meta: model = examplemodel

the form class handle creation of input element is_ok field, other fields, override settings on model - won't disabled more (see docs more info).

python django

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -