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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:57:34+00:00 2026-05-29T07:57:34+00:00

The sql query: select id,title from blog_post inner join (select object_pk, max(submit_date) from django_comments

  • 0

The sql query:

select id,title 
from blog_post inner join (select object_pk, max(submit_date) 
                           from django_comments 
                           group by object_pk 
                           order by submit_date DESC) as foo 
where foo.object_pk=blog_post.id;

How can i get the same result using django model.objects.get function? How can i write custom query with django for this? Is there anything like

model.objects.getwithsqlquery('''select id,title 
from blog_post inner join (select object_pk, max(submit_date) 
                           from django_comments 
                           group by object_pk 
                           order by submit_date DESC) as foo 
where foo.object_pk=blog_post.id;''')

mysql tables are:

mysql> describe blog_post;
+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_id  | int(11)      | NO   | MUL | NULL    |                |
| title    | varchar(100) | NO   |     | NULL    |                |
| content  | longtext     | NO   |     | NULL    |                |
| created  | date         | NO   |     | NULL    |                |
| modified | date         | NO   |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

mysql> describe django_comments;
+-----------------+--------------+------+-----+---------+----------------+
| Field           | Type         | Null | Key | Default | Extra          |
+-----------------+--------------+------+-----+---------+----------------+
| id              | int(11)      | NO   | PRI | NULL    | auto_increment |
| content_type_id | int(11)      | NO   | MUL | NULL    |                |
| object_pk       | longtext     | NO   |     | NULL    |                |
| site_id         | int(11)      | NO   | MUL | NULL    |                |
| user_id         | int(11)      | YES  | MUL | NULL    |                |
| user_name       | varchar(50)  | NO   |     | NULL    |                |
| user_email      | varchar(75)  | NO   |     | NULL    |                |
| user_url        | varchar(200) | NO   |     | NULL    |                |
| comment         | longtext     | NO   |     | NULL    |                |
| submit_date     | datetime     | NO   |     | NULL    |                |
| ip_address      | char(15)     | YES  |     | NULL    |                |
| is_public       | tinyint(1)   | NO   |     | NULL    |                |
| is_removed      | tinyint(1)   | NO   |     | NULL    |                |
+-----------------+--------------+------+-----+---------+----------------+
13 rows in set (0.00 sec)
  • 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-29T07:57:35+00:00Added an answer on May 29, 2026 at 7:57 am

    From the docs: Performing raw SQL queries in Django

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

Sidebar

Related Questions

SQL query: SELECT ArticleCategories.Title AS Category, Articles.Title, Articles.[Content], Articles.Date FROM ArticleCategories INNER JOIN Articles
I have a following SQL QUERY: SELECT articles.name, articles.price, users.zipcode FROM articles INNER JOIN
The sql statement is like this: select posts.id, posts.title from posts inner join (select
I have a query something like this: SELECT title, desc, date FROM tablename ORDER
my sql statement is SELECT c.type,c.title,c.datereg, d.ranknum FROM T_News AS c INNER JOIN (
i have sql query select * from Roles Join Users On Roles.Role=Users.RoleId it return
Sql Query: SELECT test.* FROM test JOIN test_shares WHERE test.test_id = test_shares.test_id AND test_shares.email_address
I have this sql query: SELECT DISTINCT url FROM paragraphs WHERE MATCH (title) AGAINST('$query')
I have the SQL query: select title, scale / next_scale, c from ( select
So if I have an sql query like these: SELECT title, document FROM docs

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.