Thank you.

I'll be more specific, here is what I have:

views.py
-------------
...
if request.method == 'POST':
    some_form = SomeForm(data = request.POST, request=request,
instance=somemodel)
    ...
    if some_form.is_valid():
        some_form_update = some_form.save(commit=False)
        if some_form.cleaned_data['checkbox']:
            #do something
            ...
    return HttpResponseRedirect(reverse('app.views.somemodel_detail',
args=(somemodel.key_id,)))

else:
    some_form = SomeForm(request=request, instance = somemodel)

    return render_to_response("template.html", {'some_form':
some_form}, context_instance=RequestContext(request))



forms.py
------------
class SomeForm(ModelForm):

   def __init__(self, *args, **kw):
        self.request = kw.pop('request')
        super(SomeForm, self).__init__(*args, **kw)

   #I originally had checkbox outside of meta but have tested both
ways now
   #checkbox = forms.BooleanField(required=False, label='Checkbox')

    class Meta:
        model = SomeModel
        checkbox = forms.BooleanField(required=False,
label='Checkbox')

On template.html I have to manually include the html for the message
input of type checkbox.

When I submit the form I get the error KeyError for the form field
"checkbox" that I manually added to template.html and debug points to
the line:

if some_form.cleaned_data['checkbox']:

Thanks again for helping.


On Apr 6, 11:46 pm, raj <rajeeshrn...@gmail.com> wrote:
> On Apr 7, 8:05 am, Merrick <merr...@gmail.com> wrote:
>
> > How should I go about adding a field to a model form
> > when the field is not part of the model?
>
>  Define a Custom ModelForm by specifying model in Meta and declare the
> required addnl field there.
> (check-box means boolean, right?). Now set form attribute of your
> Admin class with the name of YourForm.
>
> Rajeesh.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to