Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

233
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda