Tom
Tom

Reputation: 47

Django Update Model Field with Variable User Input... .update(key=value)

I'm attempting to create a function to allow updates to fields based on the front-end input.

The handler would receive a profile_updates dictionary with two keys. Each will contain a list of key/value pairs.

list_of_updates['custom_permissions'] = [{"is_staff":"True"},{"other_permission":"False"}]

def update_profile(message):
    list_of_updates = message['profile_updates']
    user_update_id = message['user_id']

    for update in list_of_updates['custom_permissions']:
        for key, value in update.iteritems():
            User.objects.filter(id=user_update_id).update(key=value)

I would like to make 'key' a variable fed from the .iteritems().

Appreciate anyone's input on how, or if, this is possible.

Upvotes: 0

Views: 743

Answers (1)

Rod Xavier
Rod Xavier

Reputation: 4043

You don't need to loop through the dict. You can pass it as a kwarg expansion.

def update_profile(message):
    list_of_updates = message['profile_updates']
    user_update_id = message['user_id']

    for update in list_of_updates['custom_permissions']:
        User.objects.filter(id=user_update_id).update(**update)

Upvotes: 1

Related Questions