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!

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

14,136 Points

Model error due to __str__ issue in final Django challenge

I'm getting a strange model error in the final Django challenge. I get the same error in workspaces or on my own desktop.

The issue seems to be with the string representation of the Song class. However this shows up correctly in the admin with the format <title> by <artist>, so I'm not clear what is causing the issue?


ERROR: test_song_string (songs.tests.SongModelTests)

String version of Song should contain the title and artist

Traceback (most recent call last):
File "/home/treehouse/workspace/karaoke/songs/tests.py", line 25, in test_song_string
File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/django/db/models/manager.py",
line 127, in manager_method
return getattr(self.get_queryset(), name)(args, **kwargs)
File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/django/db/models/query.py", l
ine 346, in create
obj = self.model(
File "/usr/local/pyenv/versions/3.4.1/lib/python3.4/site-packages/django/db/models/base.py", li
ne 480, in init
raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])
TypeError: 'length' is an invalid keyword argument for this function

from django.db import models

# Create your models here.

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)
from django.shortcuts import render

from .models import Song, Performer

# Create your views here.
def song_list(request):
    songs = Song.objects.all()
    context = {'songs': songs }
    return render(request, 'songs/song_list.html', context)

def song_detail(request):
    context = {}
    return render(request, 'songs/song_detail.html', context)

def performer_detail(request):
    performer = Performer.get()
    context = {'performer': performer}
    return render(request, 'songs/performer_detail.html', context)
{% extends 'base.html' %}

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

{% block content %}
<h2>{{ performer }}</h2>
{% endblock %}
14,136 Points

Hmm not sure if because I am returning a string rather than an object to be converted to a string is the issue. But even if I just replace str in the Song class with
return self.title, self.artist

I still get the same error?

2 Answers

14,136 Points

Found it - it was a red herring. Ready asked for duration for the field name but the test wanted length - changed the field name and this fixed the error-

Martin Simensen
Martin Simensen
6,190 Points

Thanks so much for the update! Struggled a long time with same problem.

Chase Marchione
Chase Marchione
155,055 Points

Thanks for sharing this! That's an intriguing hiccup in the field naming specifications.