new at this -- need some guidance

29 views
Skip to first unread message

Danny Gale

unread,
Sep 10, 2011, 12:46:33 AM9/10/11
to django...@googlegroups.com
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?

Babatunde Akinyanmi

unread,
Sep 10, 2011, 12:59:42 AM9/10/11
to django...@googlegroups.com
The problem is very clear. Add a related_name argument to those fields
in your model. If you are not sure how to do this, search the
documentation for "related_name"

> uh oh. Now there's *TWO* errors!!! What am I doing wrong?
>
> --
> 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/-/1i5-cwJxoqAJ.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

Daniel Roseman

unread,
Sep 10, 2011, 10:45:48 AM9/10/11
to django...@googlegroups.com
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.

Danny Gale

unread,
Sep 10, 2011, 2:36:48 PM9/10/11
to django...@googlegroups.com
Daniel, thank you very much. That's extremely helpful.

What I'm trying to define is that every song can be on one album, but each album can (of course) have many songs. For that, would it be better to use the ManyToMany from Album to song or FK from song to album?

Martin Melin

unread,
Sep 10, 2011, 3:49:00 PM9/10/11
to django...@googlegroups.com

If a song can only belong to one album, you don't have a many-to-many
relationship between songs and albums, you have a many-to-one
relationship. In Django, you use ForeignKey to define this (on the
Song model).

Check out https://docs.djangoproject.com/en/dev/topics/db/models/#many-to-one-relationships
if you're curious.

Cheers,
Martin Melin

Felipe Lopez

unread,
Sep 10, 2011, 3:50:35 PM9/10/11
to django...@googlegroups.com
2011/9/10 Danny Gale <danny....@gmail.com>

Daniel, thank you very much. That's extremely helpful.

What I'm trying to define is that every song can be on one album, but each album can (of course) have many songs. For that, would it be better to use the ManyToMany from Album to song or FK from song to album?

FK from Song to Album.

The Django Tutorial has a good example on how to use this relationship: a Poll has many Choices.

https://docs.djangoproject.com/en/1.3/intro/tutorial01/


--
Luis Felipe López Acevedo
IntrosMedia

Reply all
Reply to author
Forward
0 new messages