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) > > I don't know why? Or is it a bug of django?
No it's defenitively not a bug. By default fields are restricted to be not null (which is a wisely chosen default if you ask me). If you want to allow songs that don't belong to any album you can change the declaration into: album = models.ForeignKey(Album, null=True, blank=True) This will allow NULLs in the db (null=True) and allow an empty album field to pass validation (blank=True). If you don't set the album of a song it will equal None: >>> song, created = objalbum.song_set.get_or_create(name=name) >>> print song.album None The null and blank options are described here: http://www.djangoproject.com/documentation/model_api/ http://www.b-list.org/weblog/2006/06/28/django-tips-difference-between-blank-and-null 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---