Calculate the stock count

wow! that’s one handy tool…
the value of field u pass has to be static/constant, how would I pass value which differs by item in the query.
eg: my query has 5 items and if I annotate a field with has 5 different values then how would I pass that?

No, the value doesn’t need to be a constant.

The annotate docs show how you can assign the value from a query expression, which includes the ability to refer to other fields, or perform aggregations on related fields.

the cart calculation is done in the cart model, and for that to work I need to get the duration in the backend, how can I pass the duration from the form/views to the backend to calculate the price?

The form and the views are in the “backend”. It’s the view that processes the form when the form is submitted by the browser - that’s why in the typical function based-view you look to see whether the view is invoked with a GET or a POST. The GET is used to render an empty form, the POST occurs when the submit button is clicked in the browser and sends the completed form back to the server.

Everything you write for Django in Python is running as part of your server’s process.

but still how will I pass the duration back to the model function?

In part, it depends upon what you’re going to be doing with the results of the calculations. (If you’re using this to update the same model you’re calculating this on, my answer is different than if you’re just looking to calculate the price for display and not storing it in a table.)

If all I’m doing is calculating the prices for display, my first cut at this is going to be the simplest possible solution.

So lets say we have a view that has accepted a POST of a form from which you calculate the duration. The view then needs to query your table to get a list of items that calculate price = unit_price * duration.

So in my view, I’ve already got something like:
item_list = TableName.objects.filter(<some sort of filters applied>)

Being precise here, item_list is not a list. It’s a QuerySet. No actual query has been executed yet, the query is still being prepared. (See When QuerySets are evaluated to see when the query actually gets executed.)

Anyway, this means that I can apply my annotation in the view, using whatever variables are in scope in my view.
(Or something close to this, I’m just kinda winging this at the moment.)
And, I still haven’t yet resolved this QuerySet, so I could apply additional annotations to add more variables to the models.