In the end, thanks to Ken’s assistance, this is what I came up with. Nicer code, runs fast, uses ORM.
# noinspection PyMethodMayBeStatic
def post(self, request, *args, **kwargs):
data = request.data
serializer = serializers.FixStaffingChangeReportHireFlagsSerializer(data=data)
if not serializer.is_valid():
msg = f'Error processing input: {serializer.errors}'
logging.info(msg)
return resp(
'StaffingChangeReportFixFlags_processing_error',
'SerializerException',
msg,
data,
400,
)
# get the data from the request.data JSON
affected_field = request.data['affected_fields']
boolean_to_set = request.data['boolean_to_set']
list_of_ids = request.data['list_of_ids']
type_list = ["Hire", "Contract Contingent Worker"]
# do some validation not covered by the serializer check above
if affected_field not in ('hire', 'hire_init', 'both'):
return Response(status=404, data={'Error': 'affected_field must be one of [hire, hire_init, both]'})
# not letting users pass in actual column names.
# Setting up dict of dicts for keyword arguments (kwargs) in the filter line
field_name_dict = {"both": {'hire_process_is_completed': boolean_to_set, 'hire_init_process_is_completed': boolean_to_set},
'hire': {'hire_process_is_completed': boolean_to_set},
'hire_init': {'hire_init_process_is_completed': boolean_to_set}}
# get the keyword arguments dict appropriate to the passed-in value for affected_field
kwargs = field_name_dict[affected_field]
# TODO: wrap with a try/catch and add a return for a 200 if all is well
StaffingChangeReport.objects.filter(payroll_code__in=list_of_ids, business_process_type__in=type_list).update(**kwargs)
It’s not complete yet, but it works against the DB which was the main point of this question.
Thanks again Ken!