Re: how to add a field to a model form when the field is not in the model
Found the problem, thanks again for all of the help. It turns out you have to define the extra field above def __init__(self, *args, **kw) as in: forms.py class SomeForm(ModelForm): checkbox = forms.BooleanField(required=False, widget=forms.CheckboxInput(), label='some label') def __init__(self, *args, **kw): self.request = kw.pop('request') super(SomeForm, self).__init__(*args, **kw) On Apr 7, 10:19 am, Merrickwrote: > I appreciate all of the help, I was actually showing both Daniel and > Raj that their suggestions have been tried to no result. To answer > your question, look up at Raj's suggestion. My code before and after > trying the suggestions above, is to have message defined outside of > meta. > > Do you have a suggestion aside from that? > > Thanks. > > On Apr 7, 10:07 am, Tom Evans wrote: > > > > > On Wed, Apr 7, 2010 at 5:30 PM, Merrick wrote: > > > 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') > > > This is wrong. Where did you see to put fields in a form in the Meta class? > > > class FooForm(forms.ModelForm): > > this_field_aint_on_the_model = forms.CharField(max_length=64) > > class Meta: > > model = Foo > > > Tom -- 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.
Re: how to add a field to a model form when the field is not in the model
On Apr 7, 6:19 pm, Merrickwrote: > I appreciate all of the help, I was actually showing both Daniel and > Raj that their suggestions have been tried to no result. To answer > your question, look up at Raj's suggestion. My code before and after > trying the suggestions above, is to have message defined outside of > meta. > > Do you have a suggestion aside from that? > > Thanks. No other suggestions are needed, because Tom's answer is the correct one. And I'm also not sure why you say you need to manually include the html for the checkbox field, you should be able to output it exactly the same as any other form field. Are you sure the form is valid when you try and access cleaned_data? Again, it would be helpful to see what you're doing in the view. -- DR. -- 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.
Re: how to add a field to a model form when the field is not in the model
I appreciate all of the help, I was actually showing both Daniel and Raj that their suggestions have been tried to no result. To answer your question, look up at Raj's suggestion. My code before and after trying the suggestions above, is to have message defined outside of meta. Do you have a suggestion aside from that? Thanks. On Apr 7, 10:07 am, Tom Evanswrote: > On Wed, Apr 7, 2010 at 5:30 PM, Merrick wrote: > > 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') > > This is wrong. Where did you see to put fields in a form in the Meta class? > > class FooForm(forms.ModelForm): > this_field_aint_on_the_model = forms.CharField(max_length=64) > class Meta: > model = Foo > > Tom -- 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.
Re: how to add a field to a model form when the field is not in the model
On Wed, Apr 7, 2010 at 5:30 PM, Merrickwrote: > 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') This is wrong. Where did you see to put fields in a form in the Meta class? class FooForm(forms.ModelForm): this_field_aint_on_the_model = forms.CharField(max_length=64) class Meta: model = Foo Tom -- 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.
Re: how to add a field to a model form when the field is not in the model
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, rajwrote: > On Apr 7, 8:05 am, Merrick 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.
Re: how to add a field to a model form when the field is not in the model
On Apr 7, 4:05 am, Merrickwrote: > I added a checkbox form field to my template, and I even tried to add > it in my forms.py and then in my views.py I check for it like so: > > if form.cleaned_data['checkbox_field']: > code to send email... > > But when I submit the form I get a KeyError. How should I go about > adding a field to a model form when the field is not part of the > model? > > Thanks. Defining it in the form should work. What did you do? Show us the code. -- DR. -- 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.
Re: how to add a field to a model form when the field is not in the model
On Apr 7, 8:05 am, Merrickwrote: > 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.