在 django 中查询

发布于 2024-10-31 11:49:17 字数 398 浏览 1 评论 0原文

我已将有关日期的数据存储在表中,即同一个月可能有多个条目,但当我检索它们时,我需要将它们作为组合结果。那么,无论如何,我是否可以查询它来检索在 django(view) 中将范围设置为月份的一个月的数据。

编辑:

对我上一篇文章的修改。我需要做一年,但没有具体的开始日期和结束日期。我需要设置一年中几个月的范围,无论存在的天数如何。我的数据库将如下所示:

date        count1     count2  
2011/12/01,   12,          3 
2011/12/03,   3,           6

现在我需要检索 12 月份的 count1。我需要一个通用查询,而不仅仅是特定月份(例如 30、31 或 28 天)的查询。

I have stored data in the table with respect to dates ie there could be more than one entry for the same month, but when i retrieve them i need to get them as combined result. So is there anyway that i could query it to retrieve the data for a month that is set the range as month in django(view).

edit:

Modifications from my prev post. I need to do it for a year and i do not have a specific start date and end date. I need to set the range for months in a year irrespective of the number of days present. my database would look like this:

date        count1     count2  
2011/12/01,   12,          3 
2011/12/03,   3,           6

Now i need to retrieve count1 for the month of december. I need a generic query not just for a particular month with say 30 or 31 or 28 days.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一袭水袖舞倾城 2024-11-07 11:49:17

文档< /a>

假设您有一个 DateField

class MyModel(models.Model):
    my_date = models.DateField()

查询:

import datetime
startdate = datetime.date(...start of some month...)
enddate = datetime.date(...end of some month...)
my_model_in_range = MyModel.objects.filter(my_date__range(startdate,enddate))

编辑:

计数文档

import datetime

def first_day_of_month(month, year):
    return datetime.date(year, month, 1)

def last_day_of_month(month, year):
    return datetime.date(year, month+1, d.day) - datetime.timedelta(1)

# if you want query for jan 2009: month = 1, year = 2009

first = first_day_of_month(1, 2009)
last = last_day_of_month(1, 2009)

objects_in_jan_2009 = MyModel.objects.filter(my_date__range(first, last))

count = objects_in_jan_2009.count()

docs

Assuming you have a DateField

class MyModel(models.Model):
    my_date = models.DateField()

query:

import datetime
startdate = datetime.date(...start of some month...)
enddate = datetime.date(...end of some month...)
my_model_in_range = MyModel.objects.filter(my_date__range(startdate,enddate))

edit:

count docs

import datetime

def first_day_of_month(month, year):
    return datetime.date(year, month, 1)

def last_day_of_month(month, year):
    return datetime.date(year, month+1, d.day) - datetime.timedelta(1)

# if you want query for jan 2009: month = 1, year = 2009

first = first_day_of_month(1, 2009)
last = last_day_of_month(1, 2009)

objects_in_jan_2009 = MyModel.objects.filter(my_date__range(first, last))

count = objects_in_jan_2009.count()
若水微香 2024-11-07 11:49:17

收到范围后,使用以下内容查询模型:

YouModel.objects.filter(date__gt='initial date').filter(date__lt='final date')

日期应该是模型中的一个字段。 __gt 和 __lt 对字段应用过滤器来搜索大于和小于传递的参数的值。

After you receive you range, query you model with the following:

YouModel.objects.filter(date__gt='initial date').filter(date__lt='final date')

date should be a field in your model. the __gt and __lt aply a filter for the field to search for values greater and lesser than the parameter passed.

悲喜皆因你 2024-11-07 11:49:17

最好的方法是通过原始 sql

YourModel.objects.extra(
        select={'values':'select sum(count) as count , extract(month from mymodel_db.date) as date group by date'}

Best way to do it is through raw sql

YourModel.objects.extra(
        select={'values':'select sum(count) as count , extract(month from mymodel_db.date) as date group by date'}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文