I had a similar issue, and after lurking around a bit, banging the
head on the table, etc, I figured out I could make a form, set the
fields to non-editable, use the form.save(commit=False) option, and do
exactly what I needed.
I was creating a form for a child model,and wanted to fill in the
relationship (parent) field/ID myself.
Heres a sample of what I did on an update routine (handles adds and
edits in same code):
def update1(request, portid, holdid=None):
objportfolio = Portfolio.objects.get(pk=portid)
if holdid is None:
# Add function
modelform = form_for_model(Holding, form=HoldingForm)
form = modelform()
else:
instance = Holding.objects.get(id__exact=holdid)
modelform = form_for_instance(instance, form=HoldingForm)
form = modelform(instance.__dict__)
if request.POST:
form = modelform(request.POST)
if form.is_valid():
obj = form.save(commit=False)
obj.portfolio = objportfolio
obj.save()
return HttpResponseRedirect('/holding')
return render_to_response('holding/update.html', {'form': form} )
Hope this helps.
John
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---