On 31/07/07, Gary Wilson <[EMAIL PROTECTED]> wrote:
[...]
> The save_m2m() seems a bit weird to me too.  Sometimes it's there,
> sometimes it's not.  Also, this sort of logic seems to me like it should
> be in the Model class instead.  After all, it's not the form that has
> the problem with not saving m2m data, the Model instances can't do it
> themselves.
>
> What if we let the Model instance temporarily store any many-to-many
> fields' data until the instance gets saved.  So with models like:
>
> class Author(models.Model):
>     name = CharField()
>
> class Book(models.Model):
>     title = CharField()
>     authors = ManyToManyField(Author)
>
> you could do:
>
> authors = Author.objects.filter(name__contains="Gary")
> new_book = Book(title="Hello World", authors=authors)
> new_book.save()
>
> Where save() would first save the Book and then save the Book-Author
> relationships.
>
> To implement this, maybe ReverseManyRelatedObjectsDescriptor.__set__
> could store the related objects somewhere in the passed Model instance
> instead of calling manager.clear() and manager.add().  Then, let the
> Model instance's save() method do the manager.clear() and manager.add()
> after saving itself first.  This would, however, mean that code that
> sets the m2m objects:
>
> mybook.authors = [...]
>
> wouldn't hit the database until mybook.save(), a change from the current
> behavior.  I guess this would also mean that we would have to rethink
> whether or not the other manager functions (add(), remove(), etc.) would
> also not hit the database until save().

As a user I would really like this behaviour since I often want to
have an unsaved object and set x-to-many items on it before it has
ever been saved. Currently this does not work since id in the main
item is None so as soon as you assign any children you get an error.

The sort of use-case is when copying an existing item (Book) and want
to display a form to let the new copy be edited together with linked
items (Authors) but do not want to actually save anything until the
form is submitted.

-- 
Phil

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" 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-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to