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

  • Home
  • SEARCH
  • 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 6577717
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:40:27+00:00 2026-05-25T15:40:27+00:00

I have method in a cusom QuerySet that performs raw SQL query to database.

  • 0

I have method in a cusom QuerySet that performs raw SQL query to database.

class QuerySet(models.query.QuerySet):
    def get_short(self, language_code='en'):
        """Returns shortest name for given language"""
        cursor = connection.cursor()
        cursor.execute('''SELECT t.name FROM translation t, name n
                            WHERE n.id IN (%s)
                            AND t.link_id = n.id
                            AND t.lang_id = %s
                            ORDER BY CHAR_LENGTH(t.name)
                            LIMIT 1''', [','.join(["'%s'" % obj.pk for obj in self]), get_language(language_code).pk])
        name = cursor.fetchone()
        if name:
            return name[0]

But it returns empty result. cursor.fetchone() return None instead of value. When I execute raw SQL on the same database:

SELECT t.name FROM translation t, name n
                              WHERE n.id IN ('166','167')
                              AND t.link_id = n.id
                              AND t.lang_id = 40
                              ORDER BY  CHAR_LENGTH(t.name)
                              LIMIT 1

It returns properly value.

Where I wrong? Please, help!

  • 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-25T15:40:28+00:00Added an answer on May 25, 2026 at 3:40 pm

    Try without the ", ".join to format your first parameter.

    cursor.execute('''SELECT t.name FROM translation t, name n
                                WHERE n.id IN %s
                                AND t.link_id = n.id
                                AND t.lang_id = %s
                                ORDER BY CHAR_LENGTH(t.name)
                                LIMIT 1''', [tuple(obj.pk for obj in self), get_language(language_code).pk])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class with a custom each-method: class CurseArray < Array def each_safe
I have method in a class that I need to make sure is only
I have class method that returns a list of employees that I can iterate
I have a model with a custom method. Here's the example: class MyModel(models) id
I have a method that fills a ListBox with objects (custom class) internal void
I have a method that returns an array of custom class objects that are
I have the following models but I cannot get the custom method reorder_action_items to
Hei, I have a method from a class in a custom library which I
I have this: class OrderForm(ModelForm): class Meta: model = Order exclude = ('number',) def
I have a method that receives many different kinds of objects and decides what

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.