[Answered ]-Saving values to django model through a for loop

1👍

You can use the built-in function setattr to set an attribute of your instance by name. Iterate over the keys and values in your dict and use the keys as the attribute names when calling setattr

if form.is_valid()
    form = form.save(commit=False)
    form.user = request.user
    for field_name, value in scores_dict.items():
        setattr(form, field_name, value)
    form.save()

Leave a comment