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

  • Home
  • SEARCH
  • 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 7735737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:34:33+00:00 2026-06-01T07:34:33+00:00

Well this is a simple question i want to filter two elements sorted reverse

  • 0

Well this is a simple question i want to filter two elements sorted reverse by date.

The model is:

Class ModelName(models.Model):
    usr = models.ForeignKey(UserProfile)
    created = models.DateTimeField(default=datetime.datetime.now)

The way i do it is:

ModelName.object.filter(param=param).order_by('-created')[:2]

As per my understanding what i think happens behind the scene is:

  1. All the objects from ModelName are filtered

  2. they are sorted

  3. only two of them are selected.

Maybe i am wrong, if not then how can i filter only last two elements based on the date.

  • 1 1 Answer
  • 1 View
  • 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-01T07:34:34+00:00Added an answer on June 1, 2026 at 7:34 am
    • All the objects from ModelName are filtered
    • they are sorted
    • only two of them are selected.

    Yes, but that’s done by the database, not by python. See querysets are lazy. So, that actually works pretty well since all this python:

    qs = ModelName.object.filter(param=param).order_by('-created')[:2]
    

    Will be converted to SQL when qs records are accessed (ie. for model in qs …)

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

Sidebar

Related Questions

Well this is probably a stupid question with a simple answer but when using
This is another simple 'matrix' question in Mathematica. I want to show how I
This may well be simple, but I'm new to Drupal. The organization I work
I don't understand very well this code: var img = $('<img/>', {class: photo, src:
I have a class extends NamedParameterJdbcDaoSupport. well this superclass has a final setDataSource method
This is probably a really simple question but I can't seem to find anything
Well, very simple question. So that's good news for you, I guess. More thorough
i have a simple question. for a website, i want to create an info
Hopefully this question has a simple answer i'm overlooking! I have an IIS webserver
Hey this may be a somewhat simple question, but I'm new to Eclipse and

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.