Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9024039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:54:33+00:00 2026-06-16T05:54:33+00:00

I have a model like this: from django.core import meta class Foo(models.Model): date =

  • 0

I have a model like this:

from django.core import meta

class Foo(models.Model):
    date = models.DateTimeField()
    name = models.CharField(maxlength=100)

    def __unicode__(self):
        return self.name

    class Meta:
        get_latest_by = 'date'

And I can write

>>> from datetime import datetime
>>> a1 = Foo(name='a1', date=datetime(2012, 12, 18))
>>> a1.save()
>>> a2 = Foo(name='a2', date=datetime(2012, 12, 19))
>>> a2.save()
>>> a3 = Foo(name='a3', date=datetime(2012, 12, 20))
>>> a3.save()
>>> a4 = Foo(name='a4', date=datetime(2012, 12, 21))
>>> a4.save()

Then if I write next I get following result

>>> Foo.objects.latest()
a4

How cat I get the last 3 objects?
Something like

>>> Foo.objects.latest(count=3)
a4, a3, a2

Note: I don’t have to use sorting, like

>>> Foo.objects.all().order_by('-date')

because the datebase is very large and the query takes a long time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:54:35+00:00Added an answer on June 16, 2026 at 5:54 am

    latest() is merely a shortcut for order_by(model._meta.get_latest_by) so sorting already happens when you use it. There’s no other way either. You’ll need to use

    Foo.objects.order_by('-date')[:3]
    

    If the query is too slow, your options are to either add an index on date field or use id field instead (assuming your are using automatic ids).

    Foo.objects.order_by('-id')[:3]
    

    Please note that querysets overload the slice operation, so this will translate to a ...LIMIT 3 in the SQL rather selecting all records and then taking the first 3 on the application side.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model like this: from django.db import models class House(models.Model): address =
So I can create Django model like this: from django.db import models class Something(models.Model):
I have a model that looks like this: from django.db import models from django.contrib.auth.models
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
Previously I had a django model like this class Review(models.Model): reviewdate=models.DateField(default=date.today) description=models.TextField() author=models.ForeignKey(User,null=True) I
I have 2 django models like this: class UserProfile(models.Model): user = models.OneToOneField(User) organisation =
For example, I have a model like this: Class Doggy(models.Model): name = models.CharField(u'Name', max_length
Say I have a model like: from django.db import models USER_TYPE_CHOICES = ( (1,
I have a model that looks like this: class Item(models.Model): ... publish_date = models.DateTimeField(default=datetime.datetime.now)
I have a model like this: class Person(models.Model,Subject): name = .. The class Subject

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.