[Django]-Build When Case query in for loop django

6👍

You should create only the Whens in loop and put them inside a single Case.

whens = [When(id=id, then=Value(str(id))) for id in ids] # make a list

Employee.objects.update(
    account_type=Case(
        *whens # unpack the list
    ),
)

Leave a comment