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 trialHarin Yuwarattanaporn
8,427 PointsHow to convert and auto-generate data in same models when calling .create()?
Notice that test.py use .create() to setup DB before test.
song = Song.objects.create(
title="Don't Stop Believing",
artist="Journey",
length=250,
performer=self.performer)
At the time of .create() was called. The Songs model need to convert "length" value (250) to timedelta object and store in in DurationField. My code end up with
TypeError: int() argument must be a string or a number, not 'IntegerField'
So, How to auto-generate "duration" by converting value pass in kwargs "length"? at the time instance was created?
If the "length" attribute has to be updated in future, How to make "duration" auto-update itself?
PS: I spend sometimes to understand override init , Its just too complicated. If that is what need to be done, Please give me lot of resources and example.
from django.db import models
from datetime import timedelta
# Write your models here
class Song(models.Model):
title = models.CharField(max_length=255)
artist = models.CharField(max_length=255)
performer = models.ForeignKey('Performer')
length = models.IntegerField()
duration = models.DurationField(auto_created=True, default=timedelta(minutes=int(length)))
def __str__(self):
return str(self.title) + ' by ' + str(self.artist)
class Performer(models.Model):
name = models.CharField(max_length=255)
def __str__(self):
return str(self.name)
from django.shortcuts import render
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 = Song.objects.get(pk=pk)
return render(request, 'songs/song_detail.html', {'song':song})
def performer_detail(request, pk):
performer = Performer.objects.get(pk=pk)
return render(request, 'songs/performer_detail.html', {'performer':perfor
{% extends 'base.html' %}
{% block title %}{{ performer }}{% endblock %}
{% block content %}
<h2>{{ performer }}</h2>
{% endblock %}
1 Answer
Tatiana Vasilevskaya
Python Web Development Techdegree Graduate 28,600 PointsWhy do you need a separate duration field if you already have a length field, which holds a number of seconds the song is long?
Harin Yuwarattanaporn
8,427 PointsHarin Yuwarattanaporn
8,427 PointsYes you ask very right question. I can redesign to archive same result with less work. Thank again!