StackOverflow to the rescue: http://stackoverflow.com/questions/1059831/strange-behavior-with-modelform-and-saving
On Jun 29, 2:43 pm, Huuuze <huu...@gmail.com> wrote: > This problem is very strange and I'm hoping someone can help me. For > the sake of argument, I have a Author model with ForeignKey > relationship to the Book model. When I display an author, I would > like to have a ChoiceField that ONLY displays the books associated > with that author. As such, I override the AuthorForm.__init__() > method and I create a List of choices (tuples) based upon a query that > filters books based upon the author ID. The tuple is a composite of > the book ID and the book name (i.e., (1, 'Moby Dick')). Those > "choices" are then assigned to the ModelForm's choices attribute. > > When the form renders in the template, the ChoiceField is properly > displayed, listing only those books associated with that author. > > This is where things get weird. > > When I save the form, I receive a ValueError (Cannot assign > "u'1'":Author.book" must be a Book instance). This error makes sense > due to the FK relationship. However, if I add a "print" statement to > the code, make no other changes, and then save the record, it works. > The ValueError magically disappears. I've tried this a number of > times, ensuring I haven't inadvertently made another change, and it > works each time. > > Does anyone know what's going on here? --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---