I'm not sure if this really answers your question because it doesn't update the reference value directly. One solution I use is to have another test that calculates the historical average, then determine the difference from that test value. For this example, I am using a test to determine the PDD(10x) for a photon beam (a sub test that populates into another TG51 testlist). In the testlist I calculate what what the average value was over the last x number of years as a reference value, and compare my current value against that. Doing this is a sanity check, the user can determine pretty easily if there is a setup error for example.

Here is the code I use:
import numpy as np
from qatrack.qa.models import TestInstance, TestListInstance
from datetime import datetime
# Get the Test List Instances for this current Unit
tlis = TestListInstance.objects.filter(
unit_test_collection__unit__number=META["unit_number"],
test_list__name="TG51 PDD worksheet (Photons)",
work_completed__lt=META[
"work_started"
], # only include test list instances performed before the current work_started variable,
)
vals = []
dates = []
for tli in tlis:
# Check each test list to see it the energy matches
if (
tli.testinstance_set.get(unit_test_info__test__name="Beam Energy").get_value()
== pick_energy
):
vals.append(
tli.testinstance_set.get(unit_test_info__test__name="PDD(10)x").get_value()
)
dates.append(tli.work_started)
UTILS.set_comment(", ".join(f"{v:.2f} ({d:%Y-%m-%d})" for v, d in zip(vals, dates)))
if vals:
avg_values_historical_photons = np.mean(vals)
else:
avg_values_historical_photons = None
# Plot the data
fig = UTILS.get_figure()
axes = fig.gca()
axes.plot(dates, vals, "o", label="Historical Values")
axes.plot(
dates,
[avg_values_historical_photons for d in dates],
"--",
color="b",
label="Mean",
)
axes.plot(
dates,
[1.01 * avg_values_historical_photons for d in dates],
"--",
color="r",
label="+/- 1%",
)
axes.plot(dates, [0.99 * avg_values_historical_photons for d in dates], "--", color="r")
if pdd_10x != None:
axes.plot(
META["work_started"],
pdd_10x,
marker="o",
mec="r",
label=f"Today's Value: {pdd_10x:.3f}%",
)
axes.set_ylabel("PDD(10)x")
axes.set_xlabel("Date Work Started")
axes.set_title(f"Historical PDD(10)x Values for this Unit and Energy")
axes.legend()
UTILS.write_file("Historical Results.png", fig)
Finally, the figure that gets populated when doing the testlist:
Hope this help or gives you some ideas!
Jonathan.