Ahh! That helps. Sorry, I haven't ever used form_for_model() and
didn't recognize that IssueEntryForm in the original post was NOT a
form instance. Corrections embedded below.
On Dec 21, 8:48 am, mike <[EMAIL PROTECTED]> wrote:
> Sorry if I was unclean, I just discovered this and I am still learning
> newforms, I am using forms.form_for_model and trying to get two form
> to display in my template, only 1 is a foreign key that I used
> edit_inline with my model, I am trying to get the 'customer' field of
> my issue to be hidden and automatically be given the 'name' field of
> my customer model, when I did what you suggested I get the error
> below, my view is below, thx for the suggestion
>
> type object 'IssueForm' has no attribute 'initial'
>
> @staff_member_required
> def add_edit_customer(request, id=None):
> if id is None:
> CustomerEntryForm = forms.form_for_model(Customer)
> else:
> entry = get_object_or_404(Customer, pk=id)
> IssueEntryForm = forms.form_for_model(Issue,
> fields=('issue''customer'))
> issues = Issue.objects.filter(name=entry.name)
> CustomerEntryForm = forms.form_for_instance(entry)
> cust1 = Customer.objects.get(pk=id)
> issues = Issue.objects.filter(customer=cust1)
> IssueEntryForm.base_fields['customer'].widget =
> widgets.HiddenInput()
> IssueEntryForm.initial['customer'] = "Blah, Inc."
Sorry, my initial example was completely wrong; initial[] is part of
the form *instance*, not *class* (I'm a Python novice, so forgive me
if I botched the teminology there).
Remove that line and see correction below.
>
> if request.method == 'POST':
> form = CustomerEntryForm(request.POST)
> if form.is_valid():
> form.save()
> return HttpResponseRedirect('/customers')
> if request.method == 'POST':
> form1 = IssueEntryForm(request.POST)
> if form1.is_valid():
> form1 = form1.save(commit=False)
> form1.customer = entry.name
> form1.save()
> return HttpResponseRedirect('/customers/')
> else:
> form = CustomerEntryForm()
> form1 = IssueEntryForm()
Add here:
form1.initial['customer'] = "Blah, Inc."
Assuming your Issue model has a field named 'customer' (and it's a
text field), that will cause "Blah, Inc." to appear pre-filled-in when
the unbound form is rendered.
Taking a wild guess about your models, though, if the Issue model's
'customer' field is actually a ForeignKey to a Customer object, change
that line to:
form1.initial['customer'] = cust1
(I'm getting a bit out of my depth here, but I think that should
work.)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---