Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Test Time Congratulations!

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

Songs_performer.song_id may not be NULL

Hi there!!

Busy with the Django TDD challenge. I know that my code is not ready to pass the entire test but I'm working on it gradually. Plan to pass one test at a time.

At the moment I am failing all the tests, for the same reason which I don't understand:

Songs_performer.song_id may not be NULL

I can't copy everything that is on the Workspaces fail, not sure why so please bear with this.

This is all the code i've written. Views.py

from django.shortcuts import render, get_object_or_404

from .models import Performer, Song

def song_list(request):
    songs = Song.objects.all()
    return render(request, 'songs/song_list.html', {'songs':songs})

def song_detail(request, pk):
    song = get_object_or_404(Song, pk=pk)
    return render(request, 'songs/song_detail.html', {'song': song})

def performer_detail(request, song_pk, performer_pk):
    performer = get_object_or_404(Performer, song_id = song_pk, pk=performer.pk)
    return render(request, 'songs/performer_detail.html', {'performer':performer})

models.py

from django.db import models

# Write your models here
class Song(models.Model):
    title = models.CharField(max_length=255)
    artist = models.CharField(max_length=100)
    length = models.IntegerField()

    def __str__(self):
        return '{} by {}'.format(self.title,self.artist)

class Performer(models.Model):
    name = models.CharField(max_length=100)
    song = models.ForeignKey(Song)

    def __str__(self):
        return self.name

I have not touched the html(song_detail, song_list, performer_detail) pages yet but I expect that at least one of the tests should pass.

Please help.

2 Answers

Ryan S
Ryan S
27,276 Points

Hi Nthulane,

I think you have your foreign key relationship backwards.

The README file specifies that the Song model should have a "performer" attribute that is a foreign key to the Performer model, however you have the opposite. The instructions for the app basically imply that any Song instance will belong to only one performer. There can be many different songs belonging to a single performer, but you wouldn't have many performers belonging to a single song.

Hopefully this gets you started on debugging the song_id issue, since it is most likely caused by the reversed relationship of the models.

Good luck.

Nthulane Makgato
PLUS
Nthulane Makgato
Courses Plus Student 19,602 Points

You are right. That was a bit confusing because I thought that many songs can be performed by one performer and one performer can perform one song - many to many relationship. Thanks for the clarity.