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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:17:31+00:00 2026-05-28T07:17:31+00:00

I have an SQL query in PostgreSQL that I’d like to translate to Django.

  • 0

I have an SQL query in PostgreSQL that I’d like to translate to Django.

select * from main_document 
where id in (
    select distinct on (document_id) document_id 
    from main_tokenindex 
    where token in('token1', 'token2')
    order by document_id, relevance desc
) LIMIT 100

I have 2 tables: Document and TokenIndex. 1 to many relationship, a token can be in many documents.

I have this so far:

terms = []
ids = [doc.document_id for doc in TokenIndex.objects.filter(token__in = terms).
      distinct('document__id').order_by("-relevance")]

list(Document.objects.filter(pk__in=ids))[:max_res]

As you can see, the problem is that I’m going to the database to get the list of ids, and then going back again to get the documents. This is inefficient because I might be dealing with millions of document ids, whereas I’m only interested in a small subset (defined by the max_res variable, and LIMIT in the SQL.

How do I translate the SQL query to Django? I want the Django’s query to be like the one I wrote by hand in the sense that it only returns 100 documents for example and not 1.000.000 document ids and then 100 documents.

  • 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-28T07:17:32+00:00Added an answer on May 28, 2026 at 7:17 am
    result = Document.objects.filter(pk__in=(TokenIndex.objects.filter(token__in=terms).distinct('document').order_by('document', '-relevance').values_list('document', flat=True)[:max_res]))
    

    If you don’t want 1.000.000 document ids, just return 100, you need LIMIT in inner query, not in outer query.

    Anyway, I think if you even use LIMIT with outer query it will still translate to the SQL you want.

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

Sidebar

Related Questions

I have a simple SQL query in PostgreSQL 8.3 that grabs a bunch of
I have a problem with an SQL query on Postgresql. This select clause is
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
I have this SQL query: SELECT * FROM IMAGES WHERE IMAGENAME in ('IMG1', 'IMG2',
I have the SQL query: select title, scale / next_scale, c from ( select
In PostgreSQL, there is this very useful string_agg function, that allows query like: SELECT
Given SQL as an input, I have to query a PostgreSQL database and return
I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have a sql query that runs super fast, around one second, when not
I have a sql function that does a simple sql select statement: CREATE OR

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.