0

I have a model:

class Hero(models.Model):
    # ...
    name = models.CharField(max_length=100)
    category = models.ForeignKey(Category, on_delete=models.CASCADE)

    benevolence_factor = models.PositiveSmallIntegerField(
        help_text="How benevolent this hero is?",
        default=50
    )

and a dictionary:

sample_dict = {
1: 'A',
2: 'B',
3: 'C'
}

I tried to use annotate like below:

hero = Hero.objects.annotate(sample_id=Value(sample_dict[F('id)], output_field=CharField()))

but there is an key error <django.db.models.expressions.Subquery object at 0xffff8d014438>

I want to use value of field 'id' of Hero model as a key of sample_dict. but Subquery does not return a value.

Manh Do
  • 1
  • 1

0 Answers0