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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:50:19+00:00 2026-05-16T15:50:19+00:00

I have a custom query which eventually returns a list of objects. I need

  • 0

I have a custom query which eventually returns a list of objects. I need the function to return the actual objects but I don’t want to hit the database twice for every query since it’s already an expensive query. How can i return a model instance without hitting the db?

NB: I presume doing something like the following will actually create a new instance of a different model?

return [Object(pk=row[0]) for row in results]

NB: I also presume that this will hit the database, on function return

return [Object.objects.get(pk=row[0]) for row in results]
  • 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-05-16T15:50:20+00:00Added an answer on May 16, 2026 at 3:50 pm

    If you have Django 1.2+ you can use the raw() method to return list of Model instances using the results of a custom query. Something like this in your case:

    query = "<your query goes here>"
    Object.objects.raw(query)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have custom db class which extends MySQLi class MySQLi->query() method returns obejct with
i have created custom query which i am sending via POST ajax but problem
I have an oracle query in which and i want the result to be
I have a custom post type called song and i want to query the
I have a fairly simple requirement for a query API which I need to
I have a service which returns an Array of ObjectProxy objects. I would like
I have query which returns a count. Regardless of whether is 0 or 4
I have the following code which executes a custom query to the database. The
I have a custom SQL query that I run with this line: @avg_score =
I have custom validation rule: public function customRule($check) { } Inside this rule I

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.