Update App/routers/stocks/models.py
Browse files- App/routers/stocks/models.py +47 -14
App/routers/stocks/models.py
CHANGED
@@ -27,6 +27,29 @@ class Stock(Model):
|
|
27 |
table = "stocks"
|
28 |
|
29 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
class StockPriceData(Model):
|
31 |
id = fields.IntField(primary_key=True)
|
32 |
stock = fields.ForeignKeyField("models.Stock", related_name="price_data")
|
@@ -35,28 +58,38 @@ class StockPriceData(Model):
|
|
35 |
closing_price = fields.DecimalField(max_digits=15, decimal_places=2)
|
36 |
high = fields.DecimalField(max_digits=15, decimal_places=2)
|
37 |
low = fields.DecimalField(max_digits=15, decimal_places=2)
|
38 |
-
volume = fields.BigIntField()
|
39 |
-
turnover = fields.BigIntField()
|
40 |
-
shares_in_issue = fields.BigIntField()
|
41 |
-
market_cap = fields.BigIntField()
|
42 |
created_at = fields.DatetimeField(auto_now_add=True)
|
43 |
|
44 |
@staticmethod
|
45 |
-
async def get_list(data):
|
46 |
-
|
47 |
-
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
async def to_dict(self):
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
|
55 |
class Meta:
|
56 |
table = "stock_price_data"
|
57 |
-
unique_together = (
|
58 |
-
("stock", "date"),
|
59 |
-
) # Prevent duplicate entries for same stock/date
|
60 |
|
61 |
|
62 |
class Dividend(Model):
|
|
|
27 |
table = "stocks"
|
28 |
|
29 |
|
30 |
+
# No changes needed for the Stock model itself
|
31 |
+
class Stock(Model):
|
32 |
+
id = fields.IntField(primary_key=True)
|
33 |
+
symbol = fields.CharField(max_length=10, unique=True)
|
34 |
+
name = fields.CharField(max_length=200)
|
35 |
+
created_at = fields.DatetimeField(auto_now_add=True)
|
36 |
+
updated_at = fields.DatetimeField(auto_now=True)
|
37 |
+
|
38 |
+
# Relationships
|
39 |
+
price_data: fields.ReverseRelation["StockPriceData"]
|
40 |
+
dividends: fields.ReverseRelation["Dividend"]
|
41 |
+
portfolio_holdings: fields.ReverseRelation["PortfolioHolding"] # Assuming this model exists
|
42 |
+
|
43 |
+
class Meta:
|
44 |
+
table = "stocks"
|
45 |
+
|
46 |
+
# Create a lean Pydantic model for Stock that excludes deep relationships
|
47 |
+
StockPydantic_Slim = pydantic_model_creator(
|
48 |
+
Stock,
|
49 |
+
name="StockPydantic_Slim",
|
50 |
+
exclude=("dividends", "portfolio_holdings", "price_data")
|
51 |
+
)
|
52 |
+
|
53 |
class StockPriceData(Model):
|
54 |
id = fields.IntField(primary_key=True)
|
55 |
stock = fields.ForeignKeyField("models.Stock", related_name="price_data")
|
|
|
58 |
closing_price = fields.DecimalField(max_digits=15, decimal_places=2)
|
59 |
high = fields.DecimalField(max_digits=15, decimal_places=2)
|
60 |
low = fields.DecimalField(max_digits=15, decimal_places=2)
|
61 |
+
volume = fields.BigIntField()
|
62 |
+
turnover = fields.BigIntField()
|
63 |
+
shares_in_issue = fields.BigIntField()
|
64 |
+
market_cap = fields.BigIntField()
|
65 |
created_at = fields.DatetimeField(auto_now_add=True)
|
66 |
|
67 |
@staticmethod
|
68 |
+
async def get_list(data: QuerySet):
|
69 |
+
# Create a Pydantic model for StockPriceData that uses our slim Stock model
|
70 |
+
PriceDataPydantic = pydantic_model_creator(
|
71 |
+
StockPriceData,
|
72 |
+
name="PriceDataWithSlimStock",
|
73 |
+
# This is the key part: we explicitly define the nested model to use
|
74 |
+
computed=("stock", StockPydantic_Slim)
|
75 |
+
)
|
76 |
+
|
77 |
+
# Create a Pydantic model for a list of the above
|
78 |
+
PriceDataListPydantic = pydantic_queryset_creator(
|
79 |
+
StockPriceData,
|
80 |
+
pydantic_model=PriceDataPydantic
|
81 |
+
)
|
82 |
+
|
83 |
+
return await PriceDataListPydantic.from_queryset(data)
|
84 |
|
85 |
async def to_dict(self):
|
86 |
+
# This can remain as is, or you can create a specific Pydantic model for it too
|
87 |
+
parser = pydantic_model_creator(StockPriceData)
|
88 |
+
return await parser.from_tortoise_orm(self)
|
89 |
|
90 |
class Meta:
|
91 |
table = "stock_price_data"
|
92 |
+
unique_together = (("stock", "date"),)
|
|
|
|
|
93 |
|
94 |
|
95 |
class Dividend(Model):
|