Re: ManyToManyField Usage

2006-07-29 Thread Martin Robinsson
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

Re: ManyToManyField Usage

2006-07-24 Thread Martin Robinsson
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

Re: Re: ManyToManyField Usage

2006-07-23 Thread Minglei Wang
Dear Martin, a little bit disagree with your opnions. please see the code, objalbum.save() song, created = objalbum.song_set.get_or_create(name=name, album=objalbum) That is to say, before get_or_create a song in album, the album is already exist and has an id. according to the definition of Fo

Re: ManyToManyField Usage

2006-07-23 Thread Martin Robinsson
Minglei Wang wrote: > [...] > when i want to get or create a song object in album like following, the > second parameter (album=objalbum) must be put on. > Otherwise it would report an error: album_id must not be null. > > song, created = objalbum.song_set.get_or_create(name=name, album=objalbum)

Re: Re: ManyToManyField Usage

2006-07-23 Thread Minglei Wang
Hi martin,   thanx your help. i solved it. I change the model: class Author(models.Model): name = models.CharField(maxlength=200) class Admin: ordering = ['-name'] def __str__(self):   return self.name   class Album(models.Model):   name 

Re: ManyToManyField Usage

2006-07-23 Thread Martin Robinsson
Hi Minglei, First of all I think I would rename "author" in Album to "authors" to reflect the fact that there can be many authors. You could use something like this: objalbum, created = Album.objects.get_or_create(name=album) objalbum.authors.add(objauthor) or objauthor.album_set.get_or_create(nam

ManyToManyField Usage

2006-07-23 Thread Minglei Wang
Hi,   I created 2 objects as following:   class Author(models.Model): name = models.CharField(maxlength=200) class Admin: ordering = ['-name'] def __str__(self): return self.name   class Album(models.Model): name = models.CharField(maxlength=200) author = models.ManyToManyField(A