Django:仅按年份值过滤日期时间字段?

发布于 2024-09-04 12:38:44 字数 951 浏览 6 评论 0原文

我试图吐出一个 django 页面,其中按创建年份列出了所有条目。例如:

2010 年:

  • 注释 4

  • 注释 5

  • 注释 6

2009 年:

  • 注释 1

    >
  • 注 2

  • 注 3

事实证明,这比我预想的要困难。数据来源的模型如下:

class Note(models.Model):
    business = models.ForeignKey(Business)
    note = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    class Meta:
        db_table = 'client_note'

    @property
    def note_year(self):
        return self.created.strftime('%Y')

    def __unicode__(self):
        return '%s' % self.note

我尝试了几种不同的方法,但似乎在每条道路上都遇到了障碍。我猜想一个有效的“group by”方法可以解决这个问题(PostGres DB Backend),但我似乎找不到任何支持它的 Django 功能。我尝试从数据库中获取各个年份,但我很难找到一种仅按年份值过滤日期时间字段的方法。最后,我尝试添加 note_year @property 但因为它是派生的,所以我无法过滤这些值。

对于一种优雅的方式来做到这一点有什么建议吗?我认为它应该非常简单,但我玩得很开心。任何想法都非常感激。

I'm trying to spit out a django page which lists all entries by the year they were created. So, for example:

2010:

  • Note 4

  • Note 5

  • Note 6

2009:

  • Note 1

  • Note 2

  • Note 3

It's proving more difficult than I would have expected. The model from which the data comes is below:

class Note(models.Model):
    business = models.ForeignKey(Business)
    note = models.TextField()
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    class Meta:
        db_table = 'client_note'

    @property
    def note_year(self):
        return self.created.strftime('%Y')

    def __unicode__(self):
        return '%s' % self.note

I've tried a few different ways, but seem to run into hurdles down every path. I'm guessing an effective 'group by' method would do the trick (PostGres DB Backend), but I can't seem to find any Django functionality that supports it. I tried getting individual years from the database but I struggled to find a way of filtering datetime fields by just the year value. Finally, I tried adding the note_year @property but because it's derived, I can't filter those values.

Any suggestions for an elegant way to do this? I figure it should be pretty straightforward, but I'm having a heckuva time with it. Any ideas much appreciated.

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

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

发布评论

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

评论(3

超可爱的懒熊 2024-09-11 12:38:44

构建自定义 SQL 或使用

date_list = Note.objects.all().dates('created', 'year')

for years in date_list:
    Note.objects.filter(created__year = years.year)

这是在基于日期的通用视图中完成的方式。

Either construct custom SQL or use

date_list = Note.objects.all().dates('created', 'year')

for years in date_list:
    Note.objects.filter(created__year = years.year)

This is the way it is done in date based generic views.

梦纸 2024-09-11 12:38:44

您可以使用 django.views.generic。 date_based.archive_year 或使用 year 字段抬头。

You can use django.views.generic.date_based.archive_year or use year field lookup.

好菇凉咱不稀罕他 2024-09-11 12:38:44

我们可以使用年份列表来改进过滤器,然后检查创建年份是否在其中。

使用该列表,数据库将被调用一次,因为 Note.objects.filter 将在 for 循环之外运行。

像这样的事情:

notes = Note.objects.all().dates('created', 'year')
years = [note.year for note in notes]

Note.objects.filter(created__year__in = years)

We can improve the filter using a list of years, then check if the year of creation is on that.

With that list, the database will be called once one time, because the Note.objects.filter will runs outside the for loop.

Something like this:

notes = Note.objects.all().dates('created', 'year')
years = [note.year for note in notes]

Note.objects.filter(created__year__in = years)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文