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 8144535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:24:49+00:00 2026-06-06T13:24:49+00:00

I want to achieve something relatively simple: I want to retrieve all objects from

  • 0

I want to achieve something relatively simple:
I want to retrieve all objects from my model given a range of ids
(for eg, retrieve the lines 5 to 10 from a book’s chapter).

Right now in my views.py, I’ve:

def line_range(request, book_id, chapter_id, line_start, line_end):
   book_name = get_object_or_404(Book, id = book_id)
   chapter_lines = []
   for i in range (int(line_start), int(line_end)+1):
      chapter_lines .append(Line.objects.get(book = book_id, chapter = chapter_id, line = i))
return render_to_response('app/book.html', {'bookTitle': book_name, 'lines': chapter_lines })

Now, this is obviously not the most optimized way of doing things, as it would do n database queries, when it could be done in only one.
Is there a way of doing something like:

def line_range(request, book_id, chapter_id, line_start, line_end):
   book_name = get_object_or_404(Book, id = book_id)
   lines_range = range (int(line_start), int(line_end)+1)
   chapter_lines = get_list_or_404(Line, book = book_id, chapter = chapter_id, line = lines_range)
return render_to_response('app/book.html', {'bookTitle': book_name, 'lines': chapter_lines })

This would in theory generate a much better database query (1 instead of n) and should be better performance wise.
Of course, this syntax does not work (expecting an integer, not a list).

Thanks!

  • 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-06T13:24:51+00:00Added an answer on June 6, 2026 at 1:24 pm

    I think you want __range:

    Range test (inclusive).

    Example:

    start_date = datetime.date(2005, 1, 1)
    end_date = datetime.date(2005, 3, 31)
    Entry.objects.filter(pub_date__range=(start_date, end_date))

    SQL equivalent:

    SELECT ... WHERE pub_date BETWEEN '2005-01-01' and '2005-03-31';

    You can use range anywhere you can use BETWEEN in SQL — for dates, numbers and even characters.

    So yours would be, I think:

    chapter_lines = get_list_or_404(..., line__range=(int(line_start), int(line_end)+1))
    

    Likewise, you can use __lt, __gt, __lte, __gte for one-sided comparisons.

    I would encourage you to always keep a window open with the Django documentation. There is lots of great info there if you just look.

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

Sidebar

Related Questions

I want to achieve something like the following: UrlBuilder ub = new UrlBuilder(http://www.google.com/search); ub.Parameters.Add(q,request);
I want to achieve something like this in C# 3.5: public void Register<T>() :
I want to achieve something like this inside a UIView: I have created a
I want to achieve something like : $(#left).hide('slide', {direction: 'right'}, 1000) However I do
I want to achieve something like this: BEFORE: SOME_TEXT=some_more_text OTHER_TEXT=some_other_text AFTER: SOME_TEXT OTHER_TEXT I
I want to achieve something similar to what these guys do here: - (NSUInteger)countForFetchRequest:(NSFetchRequest
I want to achieve something like this in Enterprise Architect's Communication Diagrams: start() ----------------
I have a relatively simple goal: I want to create a Cocoa application which
I want to achieve something like this with my application and its keyboard. http://www.youtube.com/watch?v=ajs1p5NCNw4
I want to achieve something like this: class C { int m_nVal; public: C(int

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.