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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:03:02+00:00 2026-05-26T10:03:02+00:00

I have MySQL DB: In Django models: class Record(models.Model): schema_id = models.IntegerField() project_id =

  • 0

I have MySQL DB:

ERR

In Django models:

class Record(models.Model):
    schema_id  = models.IntegerField()
    project_id = models.IntegerField()
    name       = models.CharField(max_length=50)

class Value(models.Model):
    record    = models.ForeignKey(Record)
    key       = models.ForeignKey(Key)
    value     = models.CharField(max_length=255)

class Key(models.Model):
    name   = models.CharField(max_length=50)
    encode = models.BooleanField(default=False, blank=True)

In outputting it should looks like this:

## record.name ##
    key.name - value.value 
    key.name - value.value  
    ...

## record.name ##
    key.name - value.value 
    key.name - value.value 
    ...

I try to do it using the following code, but it’s makes too many queries to database.

#in view
records = Record.objects.filter(project_id=1)

#in template
{% for record in records %}
    <table>
        <tr>
            <td class="legend">Record:</td>
            <td>{{ record.name }}</td>
        </tr>

        {% for value in record.value_set.all %}
            <tr>
                <td class="legend">{{ value.key.name }}:</td>
                <td>{{ value.value }}</td>
            </tr>
        {% endfor %}
    </table>
{% endfor %}

Using .select_related() i get all data in 2 query, but how iterate this data in templates to get similar structure?

records = Record.objects.filter(project_id=1)
values = Value.objects.filter(record__in=records).select_related().order_by('record')
  • 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-26T10:03:03+00:00Added an answer on May 26, 2026 at 10:03 am

    Nice job for {% regroup %} tag.

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

Sidebar

Related Questions

I have the the following Django model: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) full_name
Given the Django models: class ContainerOwner(models.Model): id = models.IntegerField() class Container(models.Model): owner = models.ForeignKey(ContainerOwner)
For Django 1.1. I have this in my models.py: class User(models.Model): created = models.DateTimeField(auto_now_add=True)
I have these models class User(models.Model): user_name = models.CharField() ph_number = models.CharField() class ExamPaper(models.Model):
I have this Django model: from django.contrib.gis.db import models class Event(models.Model): address = models.TextField()
I have a model similar to the following: class Review(models.Model): venue = models.ForeignKey(Venue, db_index=True)
I have the below db model: from datetime import datetime class TermPayment(models.Model): # I
I have a charfield with the following: myString = models.CharField(max_length=50,null=True,blank=True) In many of my
I have an issue with the django.contrib.auth User model where the email max_length is
I have a Django project in which multiple processes are accessing the backend mysql

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.