if we have Pizza Toppings and Restaurant example from
Django Docs
. I wonder if I query the Pizza like the example:
t = Restaurant.objects.prefetch_related('pizzas__toppings')
how to extract the values of toppings for each Pizza. this should work:
b = t.pizza
d = b.topping
however, it does not work for me.
I have:
class Exercise(models.Model):
name = models.CharField(max_length=64)
image = models.ImageField(upload_to=content_file_name)
class Meta:
default_related_name="exercises"
def __str__(self):
return self.name
class Formula(models.Model):
reps = models.PositiveSmallIntegerField(blank = True, default=0)
sets = models.PositiveSmallIntegerField(blank = True, default=1)
secs = models.PositiveSmallIntegerField(blank = True, default=0)
exercises = models.ManyToManyField(Exercise)
class Meta:
default_related_name="formulas"
def __str__(self):
return f'{self.exercises} is {self.sets} sets, {self.reps} reps, or {self.secs} seconds.'
class Day(models.Model):
day_number = models.PositiveSmallIntegerField()
plan = models.ManyToManyField(Plan)
formulas = models.ManyToManyField(Formula)
muscles = models.ManyToManyField(Muscle)
and my QuerySet is:
today_workout = Day.objects.prefetch_related(
'plan',
'formulas__exercises',
'muscles',
).filter(day_number=day)
I have not been successful in catching the exercise name and image from today_workout
any idea what am I doing wrong?!