[Fixed]-Invert boolean field in update operations with F()

15๐Ÿ‘

โœ…

Not is not supported here. You will have to use Case When

from django.db.models import Case, Value, When

Item.objects.filter(serial__in=license_ids
).update(renewable=Case(
    When(renewable=True, then=Value(False)),
    default=Value(True))
    )
)
๐Ÿ‘คe4c5

17๐Ÿ‘

You can also do:

Item.objects.filter(serial__in=license_ids).update(renewable=Q(renewable=False))

when renewable is True => Q object condition will give False

when renewable is False => Q object condition will give True

Django docs for Q() objects.

๐Ÿ‘คRishabh Gupta

Leave a comment