I'd like this to work:
class PartnerForm(forms.ModelForm):
class Meta:
model = Partner # I expect that all fields are included in
the form (the 'id' field as well)
def partner_new(request):
form = PartnerForm()
return render_to_response('pos.html', {'form': form},
RequestContext(request))
def partner_edit(request):
form = PartnerForm(instance = request.session['partner'])
return render_to_response('pos.html', {'form': form},
RequestContext(request))
def partner_save(request):
if request.POST.get('id', None):
form = PartnerForm(request.POST, instance = get_object_or_404
(Partner, pk = request.POST['id']))
else:
form = PartnerForm(request.POST)
if form.is_valid():
form.save()
return render_to_response('pos.html', {}, RequestContext
(request))
else:
return render_to_response('pos.html', {'form': form},
RequestContext(request))
here's an excerpt of the pos.html:
{% if form %}
<form method="POST", action="{% url partner_save %}">
<table>
{{ form.as_table }}
</table>
<input type="submit" value="save" />
</form>
...
as I said: maybe I'm on the wrong track with ModelForm ...
On Jul 21, 10:22 pm, Dan Harris <[email protected]> wrote:
> What do you mean by having the ID "in" the form? I don't quite
> understand what you are trying to do.
>
> On Jul 21, 4:15 pm, mettwoch <[email protected]> wrote:
>
> > Ok, I come back to what I wrote before. If the partner already exists
> > it has an id (primary-key or whatever). If it doesn't exist it has no
> > id. I'd just like to have the id in the form. Is it a bug, is
> > something missing here or am I completely on the wrong track. That's
> > basic database form handling. Well I could fallback to using Form
> > instead of ModelForm and maybe I'll manage to get that id in the form
> > as a hidden field or whatever, but I wonder how such basic things seem
> > impossible with the ModelForm.
>
> > Thanks for Your patience
>
> > Marc
>
> > On Jul 21, 9:55 pm, Shawn Milochik <[email protected]> wrote:
>
> > > On Jul 21, 2009, at 3:39 PM, mettwoch wrote:
>
> > > > Sorry to insist, but that's what I don't understand here. How can I
> > > > know that the partner that is 'posted' is new or not? I really become
> > > > crazy here. I can't believe after all the good things I've discovered
> > > > in Django that this can be so hard.
>
> > > Okay, thanks for clarifying.
>
> > > Well, you can have the partner identified in the URL by a slug or
> > > something, for one. That's probably the easiest way. You do need to
> > > track it somehow, starting when you pull the instance from the
> > > database to populate the ModelForm. Otherwise, how can you know that
> > > it's someone new versus someone with the same details?
>
> > > The way I'm doing it is by having separate views and urls for editing
> > > and creating a new entry. But in any case you have to write something
> > > somewhere when you are pulling an existing record.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---