【发布时间】:2011-02-21 03:37:15
【问题描述】:
我正在开发一个小型健身追踪器,以便自学 Django。我想随着时间的推移绘制我的体重,所以我决定使用 Python Google Charts Wrapper。 Google 图表要求您将日期转换为 x 坐标。为此,我想通过从最后一次称重中减去第一次称重,然后使用它来计算 x 坐标(例如,我可以通过结果为 100 并增加x 坐标为每个 y 坐标的结果数。)
无论如何,我需要弄清楚如何将 Django 日期时间对象彼此相减,到目前为止,我在 google 和堆栈中都表现出色。我知道 PHP,但从未掌握过 OO 编程,所以请原谅我的无知。这是我的模型的样子:
class Goal(models.Model):
goal_weight = models.DecimalField("Goal Weight",
max_digits=4,
decimal_places=1)
target_date = models.DateTimeField("Target Date to Reach Goal")
set_date = models.DateTimeField("When did you set your goal?")
comments = models.TextField(blank=True)
def __unicode__(self):
return unicode(self.goal_weight)
class Weight(models.Model):
""" Weight at a given date and time. """
goal = models.ForeignKey(Goal)
weight = models.DecimalField("Current Weight",
max_digits=4,
decimal_places=1)
weigh_date = models.DateTimeField("Date of Weigh-In")
comments = models.TextField(blank=True)
def __unicode__(self):
return unicode(self.weight)
def recorded_today(self):
return self.date.date() == datetime.date.today()
关于如何在视图中进行的任何想法?非常感谢!
【问题讨论】:
标签: python django datetime date time