Wednesday, March 30, 2011

Re: inlineformset_factory default values

The problem I'm having is this. Those fields that I do not include in
my form attempt to update the DB, when the post data is missing those
values it blanks out those fields in the DB.

I have gotten around this by explicitly stating the fields I would
like to update but if I try to include data that cannot be updated via
the form (i.e. {{form.field.value}}) Then it will blank out the value
of that field. I don't, however, know how to access these values for
use in the template any other way. Here is my view code:


CampaignFormSet = inlineformset_factory(Race, Campaign,
can_delete=False, extra=0, max_num=0, fiel
ds=('gen_vote','name','status','party'))

r = get_object_or_404(Race, id=race_id)

if request.method == 'POST':
race_form = RaceForm(request.POST, instance=r)
if race_form.is_valid():
race_form.save(commit=False)
formset = CampaignFormSet(request.POST, instance=r)

if formset.is_valid():
race_form.save()
instances = formset.save(commit=False)
for instance in instances:

instance.save()

return HttpResponseRedirect('')

else:

formset = CampaignFormSet(instance=r)

here is the template

<form action="" method="POST">
{% for form in formset %}
{{form.name.value}}<br />
{{form.party}}
{{form.vote}}
<button type="submit"></button>
On Mar 30, 1:14 pm, Nick <nickt...@gmail.com> wrote:
> I am using inlineformsets_factory on a form and every time I save
> those fields that are not made available in the form are overridden to
> null values on save.
>
> How do i get it just to update those fields that are specifically
> listed in the form?

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

No comments:

Post a Comment