[Django]-Is there a way to negate a boolean returned to variable?

161👍

You can do this:

item.active = not item.active

That should do the trick 🙂

17👍

I think you want

item.active = not item.active
👤srgerg

14👍

item.active = not item.active is the pythonic way

10👍

Another (less concise readable, more arithmetic) way to do it would be:

item.active = bool(1 - item.active)
👤miku

7👍

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!

5👍

Its simple to do :

item.active = not item.active

So, finally you will end up with :

def toggleActive(item_id):
    item = Item.objects.get(id=item_id)
    item.active = not item.active
    item.save()

Leave a comment