Jango фильтр по последней дате в python

У меня есть следующая модель

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 монет с последним временем? Кроме того, есть ли способ фильтровать в шаблоне или альтернативно я должен связать эти 2 модели отношениями многие ко многим? Большое спасибо

Вы можете получить последние CoinsQuotes, добавив -order_by :-

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

order_by - Документация Django

Вам не нужен дополнительный код для этого, поэтому вы можете удалить приведенный выше код.

РЕДАКТИРОВАТЬ :-

В результате будет получено только 10 сообщений за сегодня (Вы также можете получить сообщения за другую дату)

from datetime import datetime

today = datetime.date.today()
latest_date = CoinsQuotes.objects.filter(coinQuotesSnapDate__gt = today).order_by('-coinQuotesSnapDate')[:10]
Вернуться на верх