[Answered ]-Iterate through fields using array Django

1👍

You likely want to mimic .update_or_create(…) [Django-doc]. You can do this with:

def upload_object_values(model, json_values, keys=None):
    model._base_manager.update_or_create(
        **{key: json_values[key] for key in keys},
        defaults={key: value for key, value in json_values.items() if key not in keys}
    )

You should here work with a reference to the model class, so not a model object:

upload_object_values(Some_model, {'field_1': 'val', 'field_2': 'val_2'}, ['field_2'])

Note: Models in Django are written in PascalCase, not snake_case,
so you might want to rename the model from Some_model to SomeModel.

Leave a comment