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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:40:17+00:00 2026-05-21T09:40:17+00:00

Am trying to query from parent to child and with a single query being

  • 0

Am trying to query from parent to child and with a single query being able to have the child results in parent. In my example, am trying to do a relation between i18n tables in which I want to include the row from the child table in a single hit.

models.py

class main(models.Model):
    slug       = models.SlugField()
    is_active  = models.BooleanField(default=True)
    site       = models.ForeignKey(Site)

    def __unicode__(self):
        return self.slug

class main_i18n(models.Model):
    main        = models.ForeignKey(main)
    language    = models.CharField(max_length=2, choices=LANGUAGES)
    title       = models.CharField(max_length=50)
    class Meta:
        unique_together = (("language", "main"))
    def __unicode__(self):
        return self.title

I tried the following to query

a=main.objects.filter(is_active=True, main_i18n__language='en')

and the relation its done perfectly, here is what I saw when tried to print the query

>>> a.query.__str__()
'SELECT `category_main`.`id`, `category_main`.`slug`, `category_main`.`is_active`, `category_main`.`site_id` FROM `category_main` INNER JOIN `category_main_i18n` ON (`category_main`.`id` = `category_main_i18n`.`main_id`) WHERE (`category_main_i18n`.`language` = en  AND `category_main`.`is_active` = True )'

As you see from the query, it does the join correctly but the column names is not included in the select statement. I cannot access the main_i18n data when trying to do a for loop.

sorry for my lousy english and hope i can find help,

Best wishes,

—- EDIT:

below is the template am currently using, ultimately I would really like to create a single query and do regrouping based to list the data in my view.

    <table class="homepage-listing">
        <tr>
            <td>
{#                {% regroup list by main as rg_main %}#}
                {% for obj in main %}
{#                    {% if obj.grouper != None %}#}
                    <div class="listing-block">
                            <a href="#" ><h3>{{ obj }}</h3></a>
                            <ul>
                                {% for s in obj.list %}
                                    <li>{{ s.list_i18n_set.get }} </li>
                                {% endfor %}
                            </ul>
                    </div>
{#                    {% endif %}#}
                {% endfor %}
            </td>
        </tr>
    </table>

— EDIT:

I had to use the extra functions to to build a single query for that purpose. I started with it but getting a bit complicated, am trying to make sure the columns not hardcoded in the queries and the joins are correct. Can anyone tell me if there is a better way to write the query?

    dict = Main.objects.extra(
    select=
    {
        'main_title' : '%s.%s' % (MainI18n._meta.db_table, MainI18n._meta.get_field('title').verbose_name),
        'list_title' : '%s.%s' % (ListI18n._meta.db_table, ListI18n._meta.get_field('title').verbose_name),
    },
    tables=[
            '%s' % (MainI18n._meta.db_table),
            '%s' % (List._meta.db_table),
            '%s' % (ListI18n._meta.db_table),
    ],
    where=[
            '%s.%s=%s.%s' % (MainI18n._meta.db_table, 'main_id', Main._meta.db_table, 'id'),
            '%s.main_id=%s.id' % (List._meta.db_table, Main._meta.db_table),
            '%s.list_id=%s.id' % (ListI18n._meta.db_table, List._meta.db_table),
            'category_listi18n.language="%s"' % (request.LANGUAGE_CODE)
    ]).select_related().filter(maini18n__language=request.LANGUAGE_CODE )
  • 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-21T09:40:18+00:00Added an answer on May 21, 2026 at 9:40 am

    You could query MainI18N instead:

    MainI18N.objects.filter(main__is_active=True, language='en').select_related('main')
    

    EDIT: You can wrap this in a custom queryset method. The quick solution (which doesn’t allow you to further filter the QuerySet) would look something like this:

    class MainQuerySet(QuerySet):
        def translate(self, lang):
           for obj in MainI18N.filter(language=lang, main__in=self).select_related('main'):
               main = obj.main
               main.i18n = obj
               yield main
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to query SQL Server database from C# I have class Class_A
i'm trying to do this query: SELECT * FROM TABLE WHERE value1='THIS'S AN EXAMPLE'.
I'm trying to get all products from the database with a single query. I
I'm trying to query a hierarchy of data in a single database table from
I'm trying to join two SQL tables, parent (which I have complete design control
I have a one-to-many mapping between a parent entity and child entities. Now I
I have 2 tables - 1 with parent records, 1 with child records. For
I have 2 tables. The parent table is learn_mor e and child table is
We are trying to create/query information from a CF based on the following structure
I'm trying to query a database from the Application_BeginRequest in my Global.asax file to

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.