I am tracking histories on my models using simple history and this works fine. Problem is, some changes made to the models are done by the application and so do not have a history_user, thus return null. The result I get form the API in this case is as below. I am trying return the id of the history_user for those that have one and set the ones that are null to a specific id, say 1. But am struggling a bit. I have added a function get_history_user to return 1 in case the value is null. But the code just runs and returns the initial result with no errors. Can this work as it is? What am I doing wrong?
Edit
I have managed to get the function to work and it returns a 1 as intended. But returning the invoice history still returns the null as below. I need some help tweaking this so that invoice_history picks the history_user from the function and appends it to the result.
{
"history_id": 509,
"history_user": null,
"history_type": "+",
"history_date": "2021-05-03T16:17:53.558643+03:00",
"id": 976
}
Views.py
class AllHistoryView(APIView):
def get(self, request, format=None):
all_invhist = Invoice.history.all()
serializers = InvoiceHistorySerializer(all_invhist, many=True)
try:
result = {}
res = serializers.data
# result = res
invoice_history = res[0]
def get_history_user(invoice_history):
history_user = invoice_history["history_user"]
if isinstance(history_user, (float, int)):
return history_user
return 1 # use 1 instead of null
rez = get_history_user(invoice_history) #rez prints 1
return Response(invoice_history, status=status.HTTP_200_OK)
except Exception as e:
error = getattr(e, "message", repr(e))
print(error)
result["errors"] = error
result["status"] = "error"
return Response(result, status=status.HTTP_400_BAD_REQUEST)