Sunday, May 29, 2011

Re: How do I override default behaviour for foreign keys in an inline formset?

Though I originally asked for a "detailed solution", even some
suggestions as to what I should be googling would be helpful at this
point. I know I'm not the only one to have tried to do this, so I
can't imagine someone doesn't have an answer somewhere.

On May 26, 12:43 am, King <sirhc...@gmail.com> wrote:
> I want to do something like this...http://groups.google.com/group/django-users/browse_thread/thread/db03...
> ...but unfortunately there was no reply to this particular posting.
> I've found other posts on the web of people looking for the same sort
> of solution, and have not been able to find any helpful answers, so I
> was hoping someone here could clear this up.
>
> For example, let's say I had an app that kept a list of books for each
> user like this:
>
> class BookList(models.Model):
>     user = models.ForeignKey(User)
>     book = models.ForeignKey(Book)
>
> class Book(models.Model):
>     author = models.ForeignKey(Author)
>     name = models.CharField(max_length=300)
>
> class Author(models.Model):
>     name = models.CharField(max_length=300)
>
> I had thought that to generate a bunch of forms for BookList (so that
> the current user could for example, enter in all of the books that
> they've read on a single page), I would use an inlineformset like so:
>
> BookListFormSet = inlineformset_factory(User, BookList)
> booklist_formset = BookListFormSet(instance=currentUser)
>
> However, the resulting formset I get results in a dropdown menu for
> each book field in the forms, but I don't want that. I want two
> TextInput widgets in the form such that the user can type out:
>    Alice in Wonderland
>    Lewis Carroll
> and if some other user has already added "Alice in Wonderland" to
> their book list, then BookList.book will just point to that book.  BUT
> if this is the first user to add "Alice in Wonderland" to their book
> list, I want the form to create a brand new row in the Book table (and
> a brand new row in Author if there is no "Lewis Carroll" books yet
> either).
>
> I'm new to Django, so maybe the solution is obvious to many.  But like
> I said, I'm finding lots of people asking this question, but without
> any good answers posted.
>
> If someone could please provide a detailed solution I would very much
> appreciate it, and I imagine others would as well.
>
> Thanks in advance.  If any part of what I'm asking isn't clear, don't
> hesitate to ask me about it.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment