On Tue, 2009-03-03 at 20:21 -0800, Margie wrote:
> I'm using formsets fairly successfully (thanks very much to Malcolm's
> great quiz example), but it seems to me there is some functionality
> missing.    Say I create a formset of BookForms, where BookForm is a
> ModelForm.  The user will filleout the formset and for each form in
> the formset, when I receive the POST data I will create an instance of
> a Book based on that data.
> 
> I can't use a modelform_factory because when I create the formset, I
> don't have any book objects yet (which I would need to set the
> queryset for the modelform_factory).

Can you provide some short code fragments? There's a lot of Modelform
and Modelformsets floating around here and it's not clear whether some
of them are typos or what's going on.

In particular, it's not clear to me why you can't do

        BookFormset = modelformset_factory(models.Book, extra=5)

or, if you have a more complex form and also wanted to, say, exclude the
"author" field:

        BookFormset = modelformset_factory(models.Book,
        my_forms.BookForm,  
            extra=5, exclude=["author"])
        
There's nothing in the code behind that which *requires* any books to
exist.

In fact, you might even want to construct a ModelFormset subclass that
prevents any existing books from being selected as part of that, if you
only want new books to be entered.


>   So instead I use a
> formset_factory that references a BookForm that is derived from a
> ModelForm.  However, when I use a formset_factory, I don't have a nice
> way to save the forms in my formset as new book objects.  For example,
> when I use just a ModelForm, I can do
> 
> bookForm = BookForm(postDict)
> newBookObj = bookForm.save()
> 
> When I use the formset, I can loop through the forms in the formset,
> but then I think I have to create the book objects myself.
> 
> for form in bookFormSet.forms:
>     newBookObj = Book(title=form.cleaned_data['title'])

Yes, if you're not using anything tied to the model, you'll need to
create them yourself. Since it's only one line of code in this
particular case, it's not really that bad, though, surely.

But it would be interesting to get some more details about why you
cannot use model formsets directly.

Regards,
Malcolm


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
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