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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:45:29+00:00 2026-05-22T19:45:29+00:00

Apologies for probably simple question, I’ve read the docs and still can’t get this

  • 0

Apologies for probably simple question, I’ve read the docs and still can’t get this working.

I’m making a raw SQL query in Django which is long so I won’t post it here – suffice it to say that it works and returns results.

I want to loop through my query results and check to see if the bandname is of the format “The [band]” and rewrite it to “[band], The”. I’m aware I could do this via SQL but the performance isn’t great for a large amount of rows, and I have a function on the band model to sort in this way, but can’t use it alongside a raw SQL query.

Here is my code:

m = Media.objects.raw('SELECT blah FROM foo')

for index, item in enumerate(m):
        if item.bandname_alt:
            if item.bandname_alt[:4] == 'The ':
                m[index].bandname_sortable = item.bandname_alt[4:] + ', The'

I know the logic works and finds the right bands, but can’t figure out how to add the bandname_sortable field to the dictionary so I can access it inside my views.

Can anyone help me out here?

  • 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-22T19:45:30+00:00Added an answer on May 22, 2026 at 7:45 pm

    First, avoid raw SQL queries. They’re rarely necessary. That’s a separate question, however.

    You have two ways to touch up your results.

    In the model. If you can find a way to avoid raw SQL, you can easily add a property to your model to handle this.

    class Media( models.Model ):
        @property
        def clean_band_name( self ):
            if self.bandname_alt[:4] == 'The ':
                return self.bandname_alt[4:] + ', The'
            else:
                return self.bandname_alt
    

    You can use sorted( list(results), key=lambda x: x.clean_band_name() )

    In the View. Build simple list of tuples or list of named tuples with your expanded results.

    data = [ (cleanup(item.bandname_alt), item) for item in m ]
    

    Since data is a simple sequence it can be sorted by the first element in each tuple.

    data.sort()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a simple one but I cant get my head around it.
Alright, this is probably gonna be a pretty simple question to answer. I haven't
This is probably a really easy question, so apologies for not being more challenging!
I am just checking out F#, so apologies if this is a silly question,
This is probably a really stupid newbie-sounding question to you developer type people, but
EDIT: I'm told that making you guys read means I get less attention. My
This is probably something very simple but I'm not getting the results I'm expecting.
apologies if this is a dupe; i couldn't find it. i've read and understood
Apologies if this question is a bit daft, but are there any negative repercussions
Apologies if this ends up being a stupid question, but I was just wondering

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.