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 trialY B
14,136 PointsModel 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?
Traceback:
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
performer=self.performer)
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(*kwargs)
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 %}
2 Answers
Y B
14,136 PointsFound 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
6,190 PointsThanks so much for the update! Struggled a long time with same problem.
Chase Marchione
155,055 PointsThanks for sharing this! That's an intriguing hiccup in the field naming specifications.
Y B
14,136 PointsY B
14,136 PointsHmm 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?