I have a simple dict containing ids with values associated like: {1:True, 2:False}
My model:
class Profile(models.Model):
user_id = models.IntegerField(unique=True, blank=True, null=True)
I also have a queryset that have the same ids listed above in an atribute named user_id
, but I need to add a new field to my objects on queryset with the values from my dict.
Lets suppose the field I want to add is called is_admin
, so I need to create a field on the object with id 1
on my queryset with the value True
.
What I tried to do is:
my_dict= {1:True, 2:False}
queryset = queryset.annotate(
is_admin=Value(my_dict.get(F("user_id")), output_field=BooleanField())
)
But what happen is that I'm receiving null
on is_admin
. I tried to do the code below and it works:
queryset = queryset.annotate(
is_admin=Value(my_dict.get(1), output_field=BooleanField())
)
So I think is something wrong with my use of F expression. I will appreciate any help.