Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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’.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

item_id = 130
field   = code_airport_from_id
value   = BCN

The dreaded yellow screen error:

enter image description here

Can this be achieved?

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading