Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

241
Views
Django queryset returns incorrect model instances but iteration works

I have a model that represents a rubric, and each rubric has multiple rows. For some reason, I run the query below, and I receive an incomplete queryset. The variable semesterrubric is a queryset of rubrics that has already been evaluated in the code and returns the correct rubric models.

"""
semesterrubric pulls both Rubric 1 and Rubric 2
Rubric 1:
    Row: 2 Row:4 
Rubric 2:
    Row: 1 Row : 1
"""
Row.objects.filter(rubric=semesterrubric)
<QuerySet [<Row: 2>, <Row: 4>]>

I know it's incomplete because when I iterate over the semesterrubric queryset object and pull the rows from each individual rubric, I receive two querysets with the rows that I need.

[rubric.row_set.all() for rubric in semesterrubric]
[<QuerySet [<Row: 2>, <Row: 4>]>, <QuerySet [<Row: 1>, <Row: 1>]>]

I would like to have a single query that returns all of the rows. What am I missing? I've read (most of) the documentation on querysets, but it is possible I missed something.

models.py

class Rubric(models.Model):
    name = models.TextField(default="Basic Rubric", unique=True)
    template = models.BooleanField(default=True)

    def __str__(self):
        return self.name

class Row(models.Model):
    CHOICES = (
    ('0', 'Your string for display'),
    ('4','Exemplary'),
    ('3','Proficient'),
    ('2','Partially Proficient'),
    ('1','Incomplete'),
    )
    name = models.CharField(default="None", max_length=100)
    rubric = models.ForeignKey(Rubric)
    row_choice = models.CharField(max_length=20,choices=CHOICES, default="0")
    excellenttext = models.TextField(default="", blank=True)
    proficienttext = models.TextField(default="", blank=True)
    satisfactorytext = models.TextField(default="", blank=True)
    unsatisfactorytext = models.TextField(default="", blank=True)
    standards = models.ManyToManyField(Standard)

    def __str__(self):
        return self.row_choice
about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I'd not recommend on passing a queryset like this. try:

Row.objects.filter(rubric__in=semesterrubric.all())
about 4 years ago · Santiago Trujillo Report

0

You received two querysets because your semesterrubric has two elements inside. Try Row.objects.filter(rubric__in=semesterrubric) This should solve your problem

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!