On Tuesday, 25 October 2011 23:56:35 UTC+1, trubliphone wrote:
>
> I am just banging my head against the wall and making no progress with 
> this issue.
>
> I am trying to create a view that nests forms of related models.  I had 
> thought that inlineformset_factory would do the trick, but I clearly 
> don't understand something.
>
> Assume I have the following models:
>
> <snip>
> class Author(models.Model):
>      name = models.CharField(max_length=100)
>
> class Book(models.Model):
>      author = models.ForeignKey(Author)
>      title = models.CharField(max_length=100)
> </snip>
>
> And the following forms:
>
> <snip>
> class AuthorForm(forms.ModelForm):
>      class Meta:
>          model = Author
>
> class BookForm(forms.ModelForm):
>      class Meta:
>          model = Book
>
> BookInlineFormSet = inlineformset_factory(Author, Book)
> </snip>
>
> Now assume I want to create a view that lets me create a new books, 
> along with a new author.  According to various documentation, I ought to 
> do something a bit like this:
>
> <snip>
> def new_author(request):
>    authorForm = AuthorForm()
>    author = Author() # an empty author
>    bookFormSet = BookInlineFormSet(instance=author) # since author is 
> empty, shouldn't this contain empty forms?
>    return render_to_response("new_author.html", {"formset" : bookFormSet, 
> })
> </snip>
>
> But all I get is a set of fields belonging to the BookForm ("title") and 
> nothing from the AuthorForm ("name").
>
> Any advice?
>
> Thanks for your help.
>
>
>
You're on the right track with inlineformset, but you still need to render 
the parent form separately - the formset is only the related items, not the 
parent. So just pass `authorForm` to the context and display it as normal 
(with `{{ authorForm.as_p }}` or by iterating through the fields).
--
DR.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/-ib-7n7o59IJ.
To post to this group, send email to django-users@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