【问题标题】:Django filter by latest date in pythonDjango按python中的最新日期过滤
【发布时间】:2021-10-11 20:48:10
【问题描述】:

我有以下型号

class CoinsQuotes(models.Model):
    coinQuotesID = models.AutoField(primary_key=True)
    coinID = models.ForeignKey(Coins, on_delete=models.CASCADE)
    coinCurrency= models.CharField(max_length=10)
    coinPrice =  models.DecimalField(decimal_places=8, max_digits=40)
    coinVolume24h = models.DecimalField(decimal_places=2, max_digits=30)
    coinPercentageChange1h =  models.DecimalField(decimal_places=2, max_digits=20)
    coinPercentageChange24h =  models.DecimalField(decimal_places=2, max_digits=20)
    coinPercentageChange7D =  models.DecimalField(decimal_places=2, max_digits=20)
    coinPercentageChange30D =  models.DecimalField(decimal_places=2, max_digits=20)
    coinPercentageChange60D =  models.DecimalField(decimal_places=2, max_digits=20)
    coinPercentageChange90D =  models.DecimalField(decimal_places=2, max_digits=20)
    coinPercentageChange180D =  models.DecimalField(decimal_places=2, max_digits=20)
    coinMarketCap =  models.DecimalField(decimal_places=2, max_digits=20)
    coinQuoteLastUpdated =  models.DateTimeField('quoteLastUpdated')
    coinQuotesSnapDate = models.DateTimeField(auto_now_add =True)

class CoinsPortfolio(models.Model):
    coinPortfolioID = models.AutoField(primary_key=True)
    coinID = models.ForeignKey(Coins, on_delete=models.CASCADE)
    coinName= models.CharField(max_length=10)
    coinUSDNotional=  models.DecimalField(decimal_places=2, max_digits=30)
    coinAmount = models.DecimalField(decimal_places=10, max_digits=30)
    coinBookPrice =  models.DecimalField(decimal_places=10, max_digits=40)
    coinMarketPrice =  models.DecimalField(decimal_places=10, max_digits=40)
    coinBookFees =  models.DecimalField(decimal_places=2, max_digits=20)
    coinBookDate = models.DateTimeField('coinBookDate')
    coinQuoteLastUpdated =  models.DateTimeField('quoteLastUpdated')
    coinQuotesSnapDate = models.DateTimeField(auto_now_add =True)

我正在使用当前视图提取所有硬币的最新报价。

     latest_date = CoinsQuotes.objects.aggregate(latest1 = Max('coinQuotesSnapDate')).['latest1']
    pct_chg = CoinsQuotes.objects.all().filter(coinQuotesSnapDate = latest_date)

出于某种原因,它只返回一件物品,而实际上有 10 个硬币的最新时间? 另外有没有办法在模板中过滤,或者我应该将这两个模型与多对多关系联系起来? 非常感谢

【问题讨论】:

  • 你能分享latest_date有什么类型和价值吗?并显示与此过滤器匹配的 10 个对象的日期?

标签: python django filter model django-queryset


【解决方案1】:

您可以通过添加-order_by 获得最新的CoinsQuotes:-

latest_date = CoinsQuotes.objects.all().order_by('-coinQuotesSnapDate')

order_by - Django Documentation

你不需要额外的代码,所以你可以删除上面的代码。

编辑:-

它只会得到今天10的帖子(你也可以从不同的日期检索)

from datetime import datetime

today = datetime.date.today()
latest_date = CoinsQuotes.objects.filter(coinQuotesSnapDate__gt = today).order_by('-coinQuotesSnapDate')[:10]

【讨论】:

  • 我认为实际日期是coinQuotesSnapDate。这就是 OP 在他的查询中使用的内容。
  • 是的! , 正确的。更新了答案
  • 好吧,问题是我想检索一个最新日期,并根据该最新日期提取所有条目。所以是的,我是 coinQuotesSnapDate,所以我需要以某种方式过滤,但过滤器只返回对象,而至少有 10 个条目匹配此日期
  • @CamilleBeyrouthy,所以你的意思是如果今天是1 January,那么只显示 1 月 1 日的 10 个帖子。
  • 嗨@PrOgRaMmEr,我的意思是最近的日期和时间,例如2021 年 8 月 7 日,下午 2:09
猜你喜欢
  • 2019-03-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-16
  • 2016-07-10
  • 2020-03-25
  • 2014-03-10
  • 2021-11-23
  • 1970-01-01
相关资源
最近更新 更多