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 = models.CharField(maxlength=200)
class Admin:
ordering = ['-name']
def __str__(self):
return self.name
class Song(models.Model):
name = models.CharField(maxlength=200)
authors = models.ManyToManyField(Author)
album = models.ForeignKey(Album)
class Admin:
ordering = ['-name']
def __str__(self):
return self.name
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)
I don't know why? Or is it a bug of
django?
Minglei
>> 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.(name=album)
>> objalbum.authors.add(objauthor)
>> or
>> objauthor.album_set.get_or_create(name=album)
>>
>>
Please see this page for more info:
>>
http://www.djangoproject.com/documentation/models/many_to_many/
>>
>>
>>
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---
- ManyToManyField Usage Minglei Wang
- Re: ManyToManyField Usage Martin Robinsson
- Re: Re: ManyToManyField Usage Minglei Wang
- Re: ManyToManyField Usage Martin Robinsson
- Re: Re: ManyToManyField Usage Minglei Wang
- Re: ManyToManyField Usage Martin Robinsson
- Re: ManyToManyField Usage Martin Robinsson