This is an excellent thread. I came here with the same questions as
Marc. My revelation seems to be that forms are for data, and saving
is control. And these two have separate concerns/goals. Such that
fields shouldn't be in forms for the sole reason to make control of
the form function properly.
So you can put the id in you forms with a hidden, or in the action=""
attribute, or using some sort of JavaScript, or whatever. So, I
understand that the key should be controlled outside the scope of the
Django form support.
But, I wonder if something like {{ form_set.management_form }} for
formsets could be used here. Perhaps something like
{{form.indentifier_key}} on a template in the form could by default
write out a hidden field of the pk of the instance. It would just be
shorthand for putting the instance in the context, and typing <input
type="hidden" name="pk" value="{{instance.id}}"..../>
But, it could make your view work like this, so you don't have to
query up the model instance yourself.
f = your_modelform(request.POST)
f.instance # would be the read from the DB and set for you.
Gene
On Jul 22, 9:23 am, Shawn Milochik <[email protected]> wrote:
> To expand on what Dan said with a full (tested and working) example:
>
> In urls.py:
>
> (r'^partner/$', 'partner_page'),
> (r'^partner/(?P<partner_id>\d+)/$', 'partner_page'),
>
> In the form tag of your template:
> <form id="approval_form" method="post" action="/partner/{% if
> partner_id %}{{ partner_id}}/{% endif %}">
>
> The view:
>
> @login_required
> def partner_page(request, partner_id = None):
>
> if partner_id:
> #existing partner
> partner_instance = get_object_or_404(partner, pk = partner_id)
> else:
> #new partner
> partner_form = partnerForm()
> partner_instance = None
>
> if request.POST:
> post_data = request.POST.copy()
> partner_form = partnerForm(post_data, instance =
> partner_instance)
>
> if partner_form.is_valid():
> the_partner = partner_form.save()
> partner_id = the_partner.id
>
> template_file = 'partner.html'
>
> context = {
> 'partner_form': partner_form,
> 'partner_id': partner_id,
> }
>
> return render_to_response(template_file, context,
> RequestContext(request))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---