【发布时间】: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