我在一个模型中有一个字段
class Sample(models.Model):
date = fields.DateField(auto_now=False)
Run Code Online (Sandbox Code Playgroud)
现在,我需要按数据范围过滤对象,例如,2011年1月1日到2011年1月31日之间的所有对象?
谢谢你的帮助!
我正在研究一个小健身追踪器,以便自学Django.我希望随着时间的推移绘制我的体重图,所以我决定使用Python Google Charts Wrapper.Google图表要求您将日期转换为ax坐标.要做到这一点,我想通过从最后一次称重中减去第一次称重然后使用它来计算x坐标来获取数据集中的天数(例如,我可以通过结果得到100并增加x坐标每个y坐标的结果数.)
无论如何,我需要弄清楚如何从彼此中减去Django日期时间对象,到目前为止,我在谷歌和堆栈中都是罢工.我知道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()
Run Code Online (Sandbox Code Playgroud)
关于如何在视图中进行的任何想法?非常感谢!