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

Harin Yuwarattanaporn
Harin Yuwarattanaporn
8,427 Points

How 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.

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

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

{% block content %}
<h2>{{ performer }}</h2>
{% endblock %}

1 Answer

Harin Yuwarattanaporn
Harin Yuwarattanaporn
8,427 Points

Yes you ask very right question. I can redesign to archive same result with less work. Thank again!