Is there a way to negate a boolean returned to variable?
Solution 1:
You can do this:
item.active = not item.active
That should do the trick :)
Solution 2:
I think you want
item.active = not item.active
Solution 3:
item.active = not item.active
is the pythonic way
Solution 4:
Another (less concise readable, more arithmetic) way to do it would be:
item.active = bool(1 - item.active)
Solution 5:
The negation for booleans is not
.
def toggle_active(item_id):
item = Item.objects.get(id=item_id)
item.active = not item.active
item.save()
Thanks guys, that was a lightning fast response!