Invert boolean field in update operations with F()

前端 未结 2 1309
臣服心动
臣服心动 2021-01-18 03:32

I need, through an update operation, revert a boolean value.

I tried:

Item.objects.filter(serial__in=license_ids).update(re         


        
相关标签:
2条回答
  • 2021-01-18 04:18

    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

    0 讨论(0)
  • 2021-01-18 04:20

    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))
        )
    )
    
    0 讨论(0)
提交回复
热议问题