Hi Minglei,
Sorry for the digression. At first I thought you only wanted
to solve
your immediate problem of how to add records to a
many-to-many relation
to be able to move on. I understand now that you also wonder
why one
has to specify the album a second time when the
objalbum.song_set
should be able to figure out by itself that a new object
should be
linked to objalbum. I agree, that's strange indeed.
Consider the
following example:
>>> song =
objalbum.song_set.create(name='new_song1')
>>> song.album == objalbum
True
>>> song =
objalbum.song_set.get_or_create(name='new_song2')
>>> print song.album
None
(or "IntegrityError: song.album_id may not be
NULL" in case NULL is not
allowed for album)
It seems that only one of these correctly handles the
relation to
objalbum. Maybe it is a bug after all... ¯\(°_o)/¯
I tested to copy the get_or_create method from QuerySet to
RelatedManager and changed:
obj = self.model(**params)
into:
obj = self.model(**params)
self.add(obj)
in RelatedManager. This seems to do the trick and also
reflects the
difference between the implementations of the create method
in the two
classes, but I don't know if it's the right way to do it
since I'm
relatively new to django. Anyone more experienced who care
to comment
on this matter?
Martin
--~--~---------~--~----~------------~-------~--~----~
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://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
|