Skip to content Skip to sidebar Skip to footer

How To Programmatically Execute When And Then On Mysql Update Using Python And Django?

I would like to update multiple rows in one single query by using UPDATE CASE scenario in mySQL. I am building my web app using python and Django. Here is my code: UPDATE order SE

Solution 1:

You can programmatically build up a query, using the conditionals Case and When.

So, for example:

from django.db.models import Case, When

# Make a list ofWhen expressions
when_list = [When(priority_number=2, then=3),
             When(priority_number=3, then=4)]

# Use in a query of your choice, as an example:
Model.objects.update(
    priority_number=Case(
        *when_list))

Post a Comment for "How To Programmatically Execute When And Then On Mysql Update Using Python And Django?"