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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:26:25+00:00 2026-05-22T12:26:25+00:00

Been writing some raw SQL queries after noticing how inefficient some of Django’s built-in

  • 0

Been writing some raw SQL queries after noticing how inefficient some of Django’s built-in queries were. I’m trying to loop through the QuerySet result and group them into categories (I’m aware of the regroup template tag, this doesn’t work for me – I need to be able to access the separate groups independently). Here’s my code:

m = Media.objects.raw('SELECT * FROM table') # query simplified for sake of example

media_items = {'aim-icons' : [], 'banners' : [], 'hi-res-photos' : [], 'photos' : [], 'print-ads' : [], 'videos' : [], 'wallpapers' : [] }

for item in m:
    media_items[item.type_slug].append(item)

This gives me what I want (eg a list that I can access like media_items['wallpapers']) but it runs a database query for every iteration to fetch the type_slug field. I tried adding m = list(m) before the loop, no effect.

Can anyone help me out here? This seems like it should be simple.

Thanks,
Matt

  • 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-22T12:26:26+00:00Added an answer on May 22, 2026 at 12:26 pm

    Edit:

    The issue breaks down here to how Django’s raw() method works. It returns model instances (which had properties you were accessing, resulting in the extra query).

    The proper tools here are connection.cursor(), cursor.execute() and cursor.fetchall(). Here’s the example from the docs:

    def my_custom_sql():
        from django.db import connection, transaction
        cursor = connection.cursor()
    
        # Data modifying operation - commit required
        cursor.execute("UPDATE bar SET foo = 1 WHERE baz = %s", [self.baz])
        transaction.commit_unless_managed()
    
        # Data retrieval operation - no commit required
        cursor.execute("SELECT foo FROM bar WHERE baz = %s", [self.baz])
        row = cursor.fetchone()
    
        return row
    

    http://docs.djangoproject.com/en/dev/topics/db/sql/#executing-custom-sql-directly

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

Sidebar

Related Questions

I've been writing some pretty long SQL queries in notepad and then pasting them
I have been writing some queries about SQL Server in order to learn. I
I am trying to learn more about the DOM and have been writing some
I have been writing some a django project / app that initiates some test
I’ve been writing some tSQLt database unit tests (via Red Gate SQL Test) on
I've been writing some jQuery functions that have JavaScript variables and looping, etc inside
I've been writing some small maintenance/viewer tools that each connect to a SQLServer (2005)
I have been writing some rspec tests, and right now I want to verify
I have been writing some code to create a byte array I will be
So i've been writing some applications in C and using OpenMP for parallelization. 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.