How to get sum of field from another doctype that has linked field

I have read How to get sum of field from child table

This is based on child table relation. However, in my scenario I have similar situation like value for an invoice and receipts for the invoice, but in the context of total allowed weight of a box and separate doctype having individual items weight. I want to have allowed weight to be calculated that does not exceed the box total weight allowed.

So the situation is first the total allowed weight of the box is entered in Box DocType. Then individual BoxItem DocType is separately entered (not as a child field) by other set of users. The BoxItem is having mandatory linked field of Box DocType. Users enter BoxItem regularly and put the weight. I need to pre-populate the weight of the next BoxItem entry with limitation that it should not exceed the total of existing BoxItems for that Box vs the that Box.total_allowed_weight. The Box could be a standard corrugated box or a wooden crate.

Is it better to use frappe sql call to loop through existing BoxItem and lookup Box.total_allowed_weight or is there another call that can loop through the BoxItem DocType to calculate the used weight and lookup the Box.total_allowed_weight.

Once the total allowed - total used is calculated, I will set value for the BoxItem weight automatically using frm.set_value.

Thanks in advance.