On Saturday, 10 September 2011 05:46:33 UTC+1, Danny wrote: > > Hi, I'm learning Django (albeit slowly) and I'm trying to set up a really > simple database. I want to have Artists, Albums, and Tracks. I want to be > able to navigate both ways in the db. So you should be able to go from > artist -> album or from album -> artist. Same thing with album and track. I > really can't figure it out. Here's the simplest test case I can come up > with: > > class Song(models.Model): > album = models.ForeignKey('Album') > > class Album(models.Model): > tracks = models.ManyToManyField(Song) > > when i run syncdb, it comes up with the following error: > music.album: Reverse query name for m2m field 'tracks' clashes with field > 'Song.album'. Add a related_name argument to the definition for 'tracks'. > > So I go back and add the related_name argument: > class Song(models.Model): > album = models.ForeignKey('Album') > > class Album(models.Model): > tracks = models.ManyToManyField(Song, related_name='album') > > and run syncdb again: > music.album: Accessor for m2m field 'tracks' clashes with field > 'Song.album'. Add a related_name argument to the definition for 'tracks'. > music.album: Reverse query name for m2m field 'tracks' clashes with field > 'Song.album'. Add a related_name argument to the definition for 'tracks'. > > uh oh. Now there's *TWO* errors!!! What am I doing wrong?
The point is that you don't need both fields. Django already provides a reverse relationship whenever you define a foreign key or manytomany, and it's this automatic attribute which is clashing. Remove the `album` FK from the Song model (after all, if there's a manytomany from Song to Album, that implies a song can be on mutliple albums, so there's no point in defining the *one* album that a song is on, which is what the FK implies). Now you can still get from Song to Album via `song.album_set.all().` -- DR. -- You received this message because you are subscribed to the Google Groups "Django users" group. To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/_ZI4AS9ovzkJ. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.