最佳答案
I have a Django site, with an Item
object that has a boolean property active
. I would like to do something like this to toggle the property from False to True and vice-versa:
def toggle_active(item_id):
item = Item.objects.get(id=item_id)
item.active = !item.active
item.save()
This syntax is valid in many C-based languages, but seems invalid in Python. Is there another way to do this WITHOUT using:
if item.active:
item.active = False
else:
item.active = True
item.save()
The native python neg()
method seems to return the negation of an integer, not the negation of a boolean.
Thanks for the help.