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 trialDavid Lin
35,864 PointsOverride Save To Sync Status and Is_Live Values
After adding the status field to the Course model class, some extra coding is required to ensure the is_live value accurately reflects the status value. For example, if status is changed from "in progress" (or "in review") to "published", is_live should correspondingly change from "False" to "True", and vice-versa.
In the video, coding in the bulk action function does take care of changing is_live to the appropriate value, but if the status value is changed individually for a course, its is_live field does not change.
After tinkering a bit, I found one solution which involved overriding the save method in the Course class. Not sure if this is the best way, though, so if anyone else has other suggestions, please share.
Also, the below apparently is not sufficient for bulk actions, so in bulk action functions, the is_live value still needs to be explicitly set to update it accordingly..
STATUS_CODES = (
('i', 'In Progress'),
('r', 'In Review'),
('p', 'Published'),
)
class Course(models.Model):
created_at = models.DateTimeField(auto_now_add=True)
title = models.CharField(max_length=255)
description = models.TextField()
teacher = models.ForeignKey(User)
subject = models.CharField(default='', max_length=100)
is_live = models.BooleanField(default=False)
status = models.CharField(max_length=1, choices=STATUS_CODES, default='i')
def __str__(self):
return self.title
def time_to_complete(self):
from courses.templatetags.course_extras import time_estimate
return '{} min'.format(time_estimate(len(self.description.split())))
def save(self, force_insert=False, force_update=False):
if self.status == 'p':
self.is_live = True
else:
self.is_live = False
super(Course, self).save(force_insert, force_update)
1 Answer
Iana Popovichenko
15,299 PointsMaybe it's better to convert the is_alive
property into a function?