Let's say we have a Notification
model with manytomany=ManyToManyField(User, blank=True)
field with User
model.
How to annotate a field is_in_manytomany
to return boolean with True and False only depending on a condition, whether self.request.user
is in Notification
ManyToMany
field or not. This code below returns weird True
and False
and also None
, regardless of the default value of BooleanField
.
user = User.objects.get(id=1)
qs = Notification.objects.all().annotate(
is_in_manytomany =ExpressionWrapper(
Q(manytomany__in = [user]),
output_field=BooleanField(default=True)
)
)
for item in qs:
print(item.is_in_manytomany)
What am I doing wrong? How do you get a True/False annotation on the given condition?