List Info

Thread: ManyToManyField Usage




ManyToManyField Usage
user name
2006-07-24 10:12:19
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-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

ManyToManyField Usage
user name
2006-07-29 10:11:29
Sorry for the bump. But I'm genuinly interested in
understanding why
it's not possible to write:
>>> album.song_set.get_or_create(name='name',
album=album)
without specifying the album explicitly as a kw argument.
Shouldn't the
album parameter in this example be inferred through the
album.song_set
reference?
Anyone who can enlighten me? Would my fix above be correct?


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Django users" group.
To post to this group, send email to django-usersgooglegroups.com
To unsubscribe from this group, send email to
django-users-unsubscribegooglegroups.com
For more options, visit this group at http://gr
oups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )