class Task(models.Model):
name = models.CharField(max_length=120)
category = models.ForeignKey('TaskCategory')
description = models.TextField()
price = models.DecimalField(max_digits=5, decimal_places=2, blank=True)
buyer = models.ForeignKey(Profile, related_name='buyer')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = _('Task')
verbose_name_plural = _('Tasks')
ordering = ['created_at']
def __unicode__(self):
return self.name
def current_status_str(self):
task_status = TaskStatus.objects.filter(task=self)
current_status = task_status.aggregate(Max('task_status'))
return TaskStatus.TASK_STATUS_VALUES[current_status['task_status__max']-1][1]
def current_status_int(self):
task_status = TaskStatus.objects.filter(task=self)
current_status = task_status.aggregate(Max('task_status'))
return current_status['task_status__max']
def get_absolute_url(self):
return reverse('tasks_details', args=[self.pk])
class TaskStatus(models.Model):
POSTED, ASSIGNED, COMPLETED, CLOSED = range(1,5)
TASK_STATUS_VALUES = (
(POSTED, 'Posted'),
(ASSIGNED, 'Assigned'),
(COMPLETED, 'Completed'),
(CLOSED, 'Closed'),
)
task = models.ForeignKey(Task)
task_status = models.IntegerField(choices=TASK_STATUS_VALUES, blank=True, null=True)
provider = models.ForeignKey(User, blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
def timesince(self, now=None):
return timesince_(self.created_at, now)
def __unicode__(self):
return unicode(self.task)
і в шаблоні звертаємось до потрібного метода
{% for task in tasks %}
{{ task }}
{{ task.current_status_str }}
{% empty %}
no tasks yet
{% endfor %}
і результат:
Немає коментарів:
Дописати коментар