In a Django app, I am running a transaction for calculating the total cost of (product) stock transfer from one station to another.
The total cost includes elements like:
a. Product cost
b. Local transportation
c. Transportation cost from station A to B
d. etc...
The cost elements depend on the geographical location of the point of dispatch, various duties and taxes (federal and state) prevailing on the date of shipment (i.e. product leaving the warehouse), transportation costs, etc. Suffice it to say that the final cost of the product (including all inputs) depends on the date of the transaction.
In order to facilitate the calculation of the best (minimum) cost of a transaction, various combinations of routes a transaction might take (for completion of the transaction) are created in the database. The logistics man is then supposed to check the best scenario on the date of dispatch using the pre-saved routes.
Based on the product and the receiving station (warehouse), a list of routes is generated, and clicking on the link against each such route, the user is taken to a screen with details of relevant elements for which the cost is sought to be calculated. On the screen, the user has to enter the date of transaction (i.e. the date on which the product movement is to take place) and all relevant costs are calculated (including the total cost) based on that date and populated in a formset (on the same screen). I am able to save the calculated costs in the underlying model.
The app is tested and is working correctly. However, while saving, the values get overwritten in the costs model. That is, for two separate transaction dates, with different underlying condition values of each cost element, get overwritten and thus defeats the purpose of saving the values. This is happening due to the fact that each route component is FK related to the header table (I surmise so).
My goal is to save the calculated costs in another model to obviate the need for a clashing FK relationship. So that each generated record of cost components is saved as an individual record set in the new model.
I have created an identical model which holds the cost data but without the FK to the header table.
Following are the cost models (the 2nd being the exact replica of the 1st without the FK relationship):
Model
class SavedCost(models.Model):
saved_cost_id = models.AutoField(primary_key=True, ...
hdr_cost_id = models.ForeignKey(RouteHeader, ...
route_cost_ref_date = models.DateField(null=True, ... ## HERE THE USER KEYS IN THE REFERENCE (TRANSACTION DATE)
transport_cost = models.DecimalField(max_digits=9, ...
taxes = models.DecimalField(max_digits=9, ...
total_cost = models.DecimalField(max_digits=9,
# ...
# Following is the replica model to store the costs calculated:
class SavedCostAll(models.Model):
saved_cost_all_id = models.AutoField(primary_key=True, ...
doc_num = models.CharField(max_length=150, null=True, blank=True,... ## The related document number from the header table is to go here
route_cost_ref_date = models.DateField(null=True, ...
transport_cost = models.DecimalField(max_digits=9, ...
taxes = models.DecimalField(max_digits=9, ...
total_cost = models.DecimalField(max_digits=9,
# ...
How can I save the displayed costs data on the form to the new model (sort of pushing the values in the new model)?