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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:50:12+00:00 2026-05-28T19:50:12+00:00

I have a pure-SQL queryset: SELECT ft2.user_id, avg(…) figure, count(…) as figure_count FROM figures_table

  • 0

I have a pure-SQL queryset:

SELECT ft2.user_id, avg(...) figure, count(...) as figure_count
FROM figures_table ft,
     figures_table ft2
WHERE ft.user_id=%(current)s AND
      ft.user_id != ft2.user_id AND
      ft2.user_id IN (%(others)s)
GROUP BY ft2.user_id

I want to make another one:

SELECT %(id)s as id,
       avg(figure * figure_count) figure
FROM (%(subquery)s) subquery

Where %(subquery)s is the first query. When I try doing commands like

User.objects.raw(second_query, {'subquery': first_qs.query})

it raises exceptions: can't adapt type QuerySet. Passing a string also doesn’t work, as it becomes quoted in the resulting query. Should I just format the string with the nested query?

  • 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-28T19:50:13+00:00Added an answer on May 28, 2026 at 7:50 pm

    Should I just format the string with the nested query?

    Yes.

    That’s essentially what raw SQL is for.

    It’s complaining because you are trying to put SQL in a variable parameter slot, and the db adapter doesnt know how to do that. (in fact, in other circumstances, what you are doing would be a SQL-injection attack, and is exactly the sort of thing that parameterized queries are designed to prevent)

    Just use regular string formatting operations to join your queries, and use that as the source for your next raw queryset.

    (do remember to escape any “%” characters in your SQL, if you are using python string interpolation on them, though)

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

Sidebar

Related Questions

I have this postgres sql query: select * from stats where athlete_id = 5
I have heard that SQL is mostly the same from program to program, but
I have the following code: mysql_query(SELECT * FROM list WHERE name LIKE '%'$searchTerm'%' OR
I have this query: SELECT COUNT(*) as clicks, DATE_FORMAT(FROM_UNIXTIME(click_date), '%w %M %Y') as point
Here's a sample of a table I have: mysql> select * from table_a where
I have a pure Winapi application that needs a few new features. One of
I have a pure C++ application developed using VC 6.0. I would like this
I have a pure Actionscript 3 project, using flash.* libraries, compiles down to about
Using pure functional languages can have a lot of benefits over using impure imperatives,
I have an abstract class defining a pure virtual method in c++: class Base

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.