how to keep track of stocks a user owns?

I am making a mock stock trading website. and for a real experience, I want users to be able to own stock and sell it.

The problem I have is I don’t know what to use or how to keep track of all the stocks a user owns.

for example, A user buys 10 Apple stock and then 10 more. Then he tries to sell 25, the website says, you only have 20. Also, the user should be able to hold multiple stocks of different number each. like 5 stocks of apple, 6 of Tesla, 18 of Ford etc. and upon buying more these number get incremented. i cant figure out how to create a model which keeps track of what and how many shares a user owns.

Probably the easiest way to handle this is through a “ManyToMany” relationship with the specification of a “through” table where that table keeps track of the number of shares owned by that person for that stock.

(Note: This becomes a bit more intricate when you need to keep track of individual transactions - in real life, you’d also want to track all the individual transactions yielding that total, but since you didn’t include that as a requirement, I’m ignoring that for now.)

okay, I will look into that! Thanks