[Fixed]-Update set of fields in django model passed in request.POST

1👍

You’d better use Forms. But if you insist on your code it could be done like this.

Suppose you have variable field_to_update where every field that you are waiting in request is listed.

subjobs_subjobid = request.POST[('subjob_id')]

field_to_update = ('subjob_name','subjob_type', 'rerun_id', 'priority_id', 'transfer_method', 'suitefile', 'topofile')
post_data = request.POST
to_be_updated = {field: post_data.get(field) for field in field_to_update if field in post_data}
# then you need to get the object, not filter it
try:
    subjobinstance = SubJobs.objects.get(id=subjobs_subjobid)
    subjobinstance.update(**to_be_updated, updated=datetime.now())
except ObjectDoesNotExist:  # from django.core.exceptions
    print('There is no such object')  # better to use logger
except Exception as e:
    print("PROBLEM UPDAING SubJob!!!!")
    print(e.message) 

Leave a comment