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
I'd not recommend on passing a queryset like this. try:
Row.objects.filter(rubric__in=semesterrubric.all())
You received two querysets because your semesterrubric has two elements inside. Try Row.objects.filter(rubric__in=semesterrubric)
This should solve your problem