Using the following model, how would I return a string 'Baked fresh every day' if the BreadSchedule object had days_available equal to every day of the week?
I want to be able to call this in my template (i.e. item.is_available_daily
).
class Item(models.Model):
name = models.CharField(max_length='250', help_text='Limited to 250 characters.')
class BreadSchedule(models.Model):
bread = models.ForeignKey(Item)
days_available = models.ManyToManyField('Day')
class Meta:
verbose_name_plural = 'Bread schedules'
def __unicode__(self):
return unicode(self.bread)
def is_available_daily(self):
"Returns whether a bread is available every day of the week."
full_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
if self.days_available.all() == full_week:
return 'Baked fresh every day'
class Day(models.Model):
day = models.CharField(max_length=50, help_text='Limited to 50 characters.')
def __unicode__(self):
return unicode(self.day)
I ended up changing the method to something simpler:
Then, in the template:
{% if bread.is_available_daily %}{{ bread.is_available_daily }}{% endif %}