Django integer models

Hello developers!
How can i do some math inside a model? i need this:

class Partite(models.Model):
	cap_assic_att = models.IntegerField(blank=True, null=True)
	cap_derogato = models.IntegerField(blank=True, null=True)
        cap_derogato2 = (cap_assic_att + ((cap_derogato/100)*cap_assic_att)) 

There is a simple way to let me have cap_derogato2 based on previous inputs?

When and where do you want to do this?

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.


1 Like

(sorry for bad indentation on original post)

Suppose we’re inside a form, the user inputs the first 2 fields:
cap_assic_att = 1000
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

There’s still ambiguity in what you want to have happen.

I navigate to your page, I see your form with the two fields.

I enter 1000 in the first field. I hit tab to go to the second field. I enter 40.

Then what? Do I hit tab to go to another field? Do I press a button? (If I press a button, what does that button do?)


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?

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.

Thanks for your patience @KenWhitesell

i think i can just do that in the html file, maybe using add? or something like 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.

Okay, trying it right now. i think i got it