If you’re wanting to have those values available when you’re working with individual instances of the model, you can create a method on that model. If you want it available in the queryset when you’re querying the data, you can annotate your queries with that value.
If you want to actually store that value in the database (generally a bad idea in my opinion, because it’s effectively replicated data within the table), you can modify your save() method to calculate the value before saving it.
Suppose we’re inside a form, the user inputs the first 2 fields:
cap_assic_att = 1000
and
cap_derogato = 40 (this should mean 40%)
what should happen now is that:
cap_derogato2 = 1000 + (0.4 * 1000) --> 1400
So the user after immitting the first 2 values should be able to see in cap_derogato2 the value of 1400.
So i need to see it in the moment of creation of that form
the cap_derogato2 field it’s not a field in which the user inputs something, it’s just a formula that uses the 2 fields above, his purpose it’s just to let the user see and copy that value.
I get that - but that doesn’t answer the question. What event occurs that will cause this value to be calculated? Is the user to press a button? Does the server need to have this value at all? Do you want the value calculated on the server or within the browser?
Ok:
Once the 2 inputs are True or Valid the page displayed must do the calculus and display it in the box, if the user want to change 1 of the 2 inputs, the displayed box must return another number based on the inputs the User gave.
So, yes, the value must be calculated and displayed, also saved inside the server, but of course every time it changes, it must be saved again, like an Update to a field i suppose.
(the user in my case doesn’t need to press the button to calculate, it will just calculate after 1 second of idle time after change the fields “cap_assic_att” and "cap_derogato ")
That’s a JavaScript issue then, not a Django issue. Once the page has been rendered and displayed to the user, Django is not involved. You’d need to write some JavaScript to detect when the page needs to be updated and then update it.
(And it’s still not clear to me why there would be any reason to save this value in the model on the server based upon everything you’ve mentioned so far.)
Because this value we’re talking about needs to be picked and reported into another file that is like a “summary”.
I still don’t get how can i manage to solve this, but i’ll discover it soon and post it.
Ok, but you don’t need to save that value to have it available at any other point of your process - it can be calculated when needed, and then written to your summary.