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 Django TDD

Nate Hawk
Nate Hawk
13,229 Points

Django Basics - Karaoke - code not passing tests in website and unable to migrate in workspace

My code does not appear to be passing the tests when I attempt a submission, I also cannot get to the testing process in the workspace because it keeps throwing this exception "AttributeError: 'int' object has no attribute 'total_seconds' ".

I am not sure what happened, but I have been trying to fix this code for over a week now and it never seems to work for me. Before I changed some things 4/5 of the tests were failing all do to songs_song.length. Frustrating process so far.

songs/models.py
from django.db import models

class Performer(models.Model):
    name = models.CharField(max_length=255)

    def __str__(self):
        return self.name


class Song(models.Model):
    title = models.CharField(max_length=255)
    artist = models.CharField(max_length=255) 
    performer = models.ForeignKey(Performer)
    duration = models.IntegerField()

    def __str__(self):
        return '{} by {}'.format(self.title, self.artist)
songs/views.py
from django.http import HttpResponse
from django.shortcuts import render, get_object_or_404
from .models import Song, Performer


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})
songs/templates/songs/performer_detail.html
{% extends 'base.html' %}

{% block title %}{{ performer }}{% endblock %}

{% block content %}
<h2>{{ performer }}</h2>
<ul>
    {% for song in performer.song_set.all %}
        <li>{{ song }}</li>
    {% endfor %}
</ul>
{% endblock %}

2 Answers

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,454 Points

The attribute 'total_seconds' is found on a timedelta object (see docs). The exception "AttributeError: 'int' object has no attribute 'total_seconds'" could mean you have an int where a timedelta object should be.

Also, be aware that while the README.TXT mentions "duration" as an field of song, the test test_song_string (karaoke/songs/tests.py) uses length as the keyword attribute for the duration. Your model will need to updated to pass the test.

Nate Hawk
Nate Hawk
13,229 Points

I fixed it, looks like a) I needed to use length instead of duration, stubbornness on my part, and b) my performer_detail view needed to look like this:

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

I was just missing the "performer.song_set.all()", however I never would have figured that out without looking at other peoples problems. I don't remember seeing anything about that in the course, so that is a little frustrating.

[MOD: added ```python markdown wrapper for formatting -cf]

Chris Freeman
Chris Freeman
Treehouse Moderator 68,454 Points

Nate, great progress so far. Are you getting through all the test yet?

in looking at your song_detail view, you are passing the string 'song' in the context instead of the variable song. Making this fix will pass the test test_song_detail_view:

python manage.py test songs.tests.ViewTests.test_song_detail_view
Creating test database for alias 'default'...
.
----------------------------------------------------------------------
Ran 1 test in 0.092s

OK