Django Relations

I am new to Django and I have several issues related to multi-volume relational systems and Manytoone (e.g. foreign key).

My setup is as follows.

I have class A, class B, class C

Each object of class B must belong to an object of class A. They cannot belong to more than one class. A more practical example would be if class A is a music group and class B is a song with that group. Most groups will have more than one song, but each song must belong to a group (in this example, a song can never have multiple groups).

Class C is a list of individual group members. Therefore, each member of the group can be associated with an arbitrary number of songs, as well as with an arbitrary number of groups. In other words, a member of Band X can also be a member of group Y.

Then my question will be

How would I use the ForeignKey and ManytoMany relationship in this context?

This example is intended only to simplify my situation and help me explain my problem. I would like the administrator to display for each object of class C objects of class B or objects of class A belonging to class C. The same applies to class B and class A.

If you look at objects of class A, you can list all objects of class B that belong to this particular object of class A.

Any and all input is appreciated.

+6
django django-models django-admin
source share
2 answers

This is how I installed it (in models.py )

 class Member(models.Model): name = models.CharField(max_length=100) ... def __unicode__(self): return self.name class Band(models.Model): name = models.CharField(max_length=100) members = models.ManyToManyField(Member) ... def __unicode__(self): return self.name class Song(models.Model): name = models.CharField(max_length=100) band = models.ForeignKey(Band) ... def __unicode__(self): return self.name 

Configure as follows:

  • member.band_set.all() gives you all the groups the member belongs to
  • band.members.all() gives you group members
  • song.band gives you a band for this song
  • band.song_set.all() gives you all the songs for the band

Notice that the band_set on and song_set on the strip are inverse relationships. They are not explicitly defined in the models, but Django customizes them transparently for you. You can customize them using the related_name parameter in the field definition. Example:

 class Band(models.Model): members = models.ManyToManyField(Member,related_name='bands') 

allows you to get all groups for a member as follows:

 member.bands.all() 

The administrator will automatically provide the following:

  • Show all group members in a multi-select list
  • Show group for composition in one selection list

However, if you want to see the songs for the group, you will need to set up the admin a bit.

In admin.py :

 from django.contrib import admin class SongInline(admin.StackedInline): model = Song extra = 1 class BandAdmin(admin.ModelAdmin): inlines = [SongInline] admin.site.register(Band,BandAdmin) admin.site.register(Member) admin.site.register(Song) 

This allows you to browse songs directly from the admin page - and edit or add them as well! Following this pattern, you can also show all groups for a member.

You can get a more detailed introduction to admin settings at http://docs.djangoproject.com/en/dev/intro/tutorial02/

+11
source share

All this is pretty simple. I used the descriptive names below to make it easier to follow.

 class Band(models.Model): name = models.CharField(max_length=255) class BandMember(models.Model): name = models.CharField(max_length=255) bands = models.ManyToManyField(Band) class Song(models.Model): name = models.CharField(max_length=255) band = models.ForeignKey(Band) # get a band myband = Band.objects.get(name='myband') # all songs from that band print myband.song_set.all() # all members of that band print myband.bandmembers.all() # get a specific band member dave = BandMember.objects.get(name='Dave') # what bands is Dave a member of? print dave.bands.all() # what songs has Dave sung? (slightly more complicated) print Song.objects.get(band__bandmember=dave) 
+3
source share

All Articles