相关疑难解决方法(0)

Django数据库查询:如何按日期范围过滤对象?

我在一个模型中有一个字段

class Sample(models.Model):
    date = fields.DateField(auto_now=False)
Run Code Online (Sandbox Code Playgroud)

现在,我需要按数据范围过滤对象,例如,2011年1月1日到2011年1月31日之间的所有对象?

谢谢你的帮助!

python django django-models django-queryset

213
推荐指数
6
解决办法
24万
查看次数

如何在Django/Python中减去两个日期?

我正在研究一个小健身追踪器,以便自学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)

关于如何在视图中进行的任何想法?非常感谢!

python django time datetime date

35
推荐指数
3
解决办法
4万
查看次数

标签 统计

django ×2

python ×2

date ×1

datetime ×1

django-models ×1

django-queryset ×1

time ×1