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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:53:01+00:00 2026-06-10T22:53:01+00:00

Djnago model: class RootTable(models.Model): id = models.IntegerField(primary_key=True) field1 = models.CharField(max_length=100, unique=True) field2 = models.CharField(max_length=120,

  • 0

Djnago model:

class RootTable(models.Model):
    id = models.IntegerField(primary_key=True)
    field1 = models.CharField(max_length=100, unique=True)
    field2 = models.CharField(max_length=120, unique=True)

class SubTableA(models.Model):
    id = models.IntegerField(primary_key=True)
    field1 = models.ForeignKey(RootTable, null=True, blank=True)
    subtableAfield1 = models.CharField(max_length=180, blank=True)

class SubTableB(models.Model):
    id = models.IntegerField(primary_key=True)
    field1 = models.ForeignKey(SubTableA, null=True, blank=True)
    subtableBfield1 = models.CharField(max_length=180, blank=True)

class SubTable2(models.Model):
    id = models.IntegerField(primary_key=True)
    field1 = models.ForeignKey(RootTable, null=True, blank=True)
    subtable2field1 = models.CharField(max_length=180, blank=True)
    subtable2field2 = models.CharField(max_length=180, blank=True)

If I use request like this:

RootTable.objects.filter( subtable2__subtable2field1 = 'text' )

It returns values only from ‘RootTable’.
SQL:

SELECT  'roottable'.'id',
        'roottable'.'field1',
        'roottable'.'field2',
FROM 'roottable'
INNER JOIN ...

But how can I get all connected values from other tables?
SQL:

SELECT  'roottable'.'id',
        'roottable'.'field1',
        'roottable'.'field2',
        'subtablea'.'subtableafield1',
        'subtableb'.'subtablebfield1',
        'subtable2'.'subtable2field1',
        'subtable2'.'subtable2field2',
FROM 'roottable', 'subtablea', 'subtableb', 'subtable2'
INNER JOIN ...

UPDATE:

I receive a response something like this:

[
{ 'roottable.id' : 'rid'},
{ 'subtablea.id' : 'id'},
{ 'subtablea.subtableafield1' : 'value1' },
...
{ 'roottable.id' : 'rid'},
{ 'subtablea.id' : 'id'},
{ 'subtablea.subtableafield1' : 'value2' },
]

But I need something like this:

[
{ 'roottable.id' : 'rid'},
{ 'subtablea.id' : 'id'},
{ 'subtablea.subtableafield1' : ['value1','value2'] },
]

is there a fast way to get such structure?

  • 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-06-10T22:53:02+00:00Added an answer on June 10, 2026 at 10:53 pm

    Django filter() query returns objects not the specific attributes. However, you can use values() to return just attributes dict. This can also take attributes of related field.

    But the side-effect is you have to pass attribute names that you want to get back (similar to SQL).

    So you can change your query as

    RootTable.objects.filter(subtable2__subtable2field1='text').values('field1', 
        'field2', 'subtablea__subtable1field1', 'subtableb__subtable2field1')
    

    More reference at Django Queryset values

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

Sidebar

Related Questions

Here is my model: class Company(models.Model): id = models.AutoField(primary_key=True); name = models.CharField(max_length=100); address =
I have a basic Django model like: class Business(models.Model): name = models.CharField(max_length=200, unique=True) email
Here is a Django model I'm using. class Person(models.Model): surname = models.CharField(max_length=255, null=True, blank=True)
I have this model in django: class JournalsGeneral(models.Model): jid = models.AutoField(primary_key=True) code = models.CharField(Code,
I have a Django model like this: class Registration(models.Model): appId =models.CharField(max_length = 100) registeredUser
I've this django model: from django.db import models class MyModel(models.Model): foo_it = model.CharField(max_length=100) foo_en
I have a django model Level for a game. class Level(models.Model): key = models.CharField(max_length=100)
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
I have a django model as following class Project(models.Model) name=models.CharField(max_length=200) class Application(models.Model) proj=models.ForeignKey(Project, null=True,
I have the following Django model: class opetest(models.Model): name = models.CharField(max_length=200) people = models.ManyToManyField(User,

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.