Benedict Verheyen wrote:
<snip>
>
> This is the code in my update view:
> 
> def action_edit(request, action_id):
>     action = get_object_or_404(Action, pk=action_id)
>     if request.method == 'POST':
>         form = ActionForm(request.POST, action)
>         if form.is_valid():
>             form.save()
>             return HttpResponseRedirect(reverse(call_detail, 
> kwargs={"call_id": action.call.id})) # Redirect after POST
>     form=ActionForm(instance=action)
>     return render_to_response('management/action_edit.html', {'form': form, 
> context_instance=RequestContext(request))
> 
> I get an IntegrityError when saving:
> 
> IntegrityError at /management/action/edit/1/
> 
> null value in column "call_id" violates not-null constraint
> 
> Regards,
> Benedict

As expected, it works if i set the values in my view:

action = get_object_or_404(Action, pk=action_id)
form=ActionForm(instance=action)
if request.method == 'POST':
    form = ActionForm(request.POST, action)
    if form.is_valid():
        edit_action=form.save(commit=False)
        edit_action.call = action.call
        edit_action.date_created = action.date_created
        edit_action.id = action.id
        edit_action.save()
        form.save_m2m()
        return HttpResponseRedirect(reverse(call_detail,
            kwargs={"call_id": action.call.id})) # Redirect after POST
return render_to_response('management/action_edit.html',
        {'form': form, 'action': action},
        context_instance=RequestContext(request))

I'm not sure if this is the best way to solve the problem.

Also, it seems strange that one needs to add the action to the ActionForm
but yet, have to set the 3 missing fields anyway (call, date_created and id)
If i don't assign the id, then a new action is made instead of an old
one update.

Could this be a bug? If not how come i need to manually set those fields
or else i get an error?

Regards,
Benedict

-- 
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