Как отобразить в шаблоне только недавно добавленный объект моей модели
как показать только недавно добавленный объект моей модели вместо всех в моем шаблоне вот мой views.py
class home(View):
def get(self, request):
quote = Quote.objects.all()
return render(request, 'home.html', {'qoutes':quote})
прямо сейчас при визуализации объекта мне будут показаны все цитаты, но вместо всех моделей я хочу визуализировать только последние цитаты, которые я добавил и получил рендер
class Quote(models.Model):
todays_Quote = models.CharField(max_length=500, blank=False)
by = models.CharField(max_length=100, blank=False)
created = models.DateTimeField(auto_now=True)
def __str__(self):
return self.todays_Quote
Вы можете получить последние 10 Quote
s с помощью:
class home(View):
def get(self, request):
quote = Quote.objects.order_by('-created')[:10]
return render(request, 'home.html', {'qoutes':quote})
или мы можем получить все Quote
, поданные, например, за последний день с помощью:
from datetime import timedelta
from django.db.models.functions import now
class home(View):
def get(self, request):
quote = Quote.objects.filter(created__gte=Now()-timedelta(days=1))
return render(request, 'home.html', {'qoutes':quote})