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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:56:17+00:00 2026-06-13T02:56:17+00:00

I have a small probleme concerning django querysets and select_related. With this model :

  • 0

I have a small probleme concerning django querysets and select_related.

With this model :

class DeviceGroup(models.Model):
  name = models.CharField(max_length=255, unique=True)
  owner = models.ForeignKey(User)

class Device(models.Model):
  name = models.CharField(max_length=255)
  address = models.GenericIPAddressField()
  port = models.IntegerField()
  group =    models.ForeignKey(DeviceGroup)

class Sensor(models.Model):
  device = models.ForeignKey(Device)

I’d like to get a list like :

- group 1
  - device 1
  - device 2
  - device 3
    -sensor 1
- group 2
- group 3
  - device 4
  - device 5
    - sensor 2

I’ve read the doc about select_related(), but I don’t know how to do because I’ve to start my query bu group cause, it’s directly linked to logged user.

  • 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-13T02:56:17+00:00Added an answer on June 13, 2026 at 2:56 am

    If you want to present it this way in a Django template, you probably want to use regroup:
    https://docs.djangoproject.com/en/dev/ref/templates/builtins/#regroup

    The new docs is slightly more obfuscate, I find the old docs version easy to comprehend (this eg from https://docs.djangoproject.com/en/1.3/ref/templates/builtins/#regroup ):

    {% regroup people by gender as gender_list %}
    
    <ul>
    {% for gender in gender_list %}
        <li>{{ gender.grouper }}
        <ul>
            {% for item in gender.list %}
            <li>{{ item.first_name }} {{ item.last_name }}</li>
            {% endfor %}
        </ul>
        </li>
    {% endfor %}
    </ul>
    

    Where in your case:

    from view.py

    group = Group.objects.all()
    

    template.html

    {% regroup groups by device as device_list %}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have small problem with simple code. This code is working properly on x86
i have small problem understanding why my smart pointer class is leaking on self
I have several newbie questions concerning eigen. Below is a small function to illustrate
I have small problem. I learn java SE and find class ClassLoader. I try
I know that this question seems to be banal, but I have small problem.
have small problem, and would very much appreciate help :) I should convert byte
I have small problem with Spring MVC. Basically what I'm trying to do is
I have a small problem and I've figured out where when and why it
I have a small problem which i can't seem to figure out. I tried
I have a small problem which i can't seem to solve myself. Look at

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.