Home > database >  Django: Database column not updating from variable, taking it as hardcoded name
Django: Database column not updating from variable, taking it as hardcoded name

Time:01-11

I have the following code for updating a database column.

def update_itemamadeus(check_flight_differences):

    for item_id, flight_details in check_flight_differences.items():

        for field, value in flight_details.items():

            ItemAmadeus.objects \
                .filter(
                    Q(id=item_id)
                ) \
                .update(
                    field = value
                )
    return

It's taking 'field' not as the variable it should be which is 'code_airport_from_id'.

item_id = 130
field   = code_airport_from_id
value   = BCN

The dreaded yellow screen error:

enter image description here

Can this be achieved?

CodePudding user response:

You need to convert the parameters as dictionary and pass it through function by unpacking them, like this:

ItemAmadeus.objects \
            .filter(
                Q(id=item_id)
            ) \
            .update(
                **{field:value}
            )

This article on geeksforgeeks has some examples of unpacking.

  • Related