Yakova commited on
Commit
554f108
·
verified ·
1 Parent(s): e5309c1

Update App/routers/stocks/models.py

Browse files
Files changed (1) hide show
  1. App/routers/stocks/models.py +28 -19
App/routers/stocks/models.py CHANGED
@@ -28,13 +28,28 @@ class Stock(Model):
28
 
29
 
30
 
31
- # Create a lean Pydantic model for Stock that excludes deep relationships
 
32
  StockPydantic_Slim = pydantic_model_creator(
33
- Stock,
34
- name="StockPydantic_Slim",
35
- exclude=("dividends", "portfolio_holdings", "price_data")
36
  )
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  class StockPriceData(Model):
39
  id = fields.IntField(primary_key=True)
40
  stock = fields.ForeignKeyField("models.Stock", related_name="price_data")
@@ -51,32 +66,26 @@ class StockPriceData(Model):
51
 
52
  @staticmethod
53
  async def get_list(data: QuerySet):
54
- # Create a Pydantic model for StockPriceData that uses our slim Stock model
55
- PriceDataPydantic = pydantic_model_creator(
56
- StockPriceData,
57
- name="PriceDataWithSlimStock",
58
- # This is the key part: we explicitly define the nested model to use
59
- computed=("stock", StockPydantic_Slim)
60
- )
61
-
62
- # Create a Pydantic model for a list of the above
63
  PriceDataListPydantic = pydantic_queryset_creator(
64
- StockPriceData,
65
- pydantic_model=PriceDataPydantic
 
66
  )
67
-
68
  return await PriceDataListPydantic.from_queryset(data)
69
 
70
  async def to_dict(self):
71
- # This can remain as is, or you can create a specific Pydantic model for it too
72
  parser = pydantic_model_creator(StockPriceData)
73
  return await parser.from_tortoise_orm(self)
74
 
75
  class Meta:
76
  table = "stock_price_data"
77
  unique_together = (("stock", "date"),)
78
-
79
-
80
  class Dividend(Model):
81
  id = fields.IntField(primary_key=True)
82
  stock = fields.ForeignKeyField("models.Stock", related_name="dividends")
 
28
 
29
 
30
 
31
+ # ----------------- STEP 1: Create the SLIM Pydantic model for Stock -----------------
32
+ # This model represents a Stock but without its own nested lists.
33
  StockPydantic_Slim = pydantic_model_creator(
34
+ Stock,
35
+ name="StockSlim",
36
+ exclude=("dividends", "portfolio_holdings", "price_data", "watching") # Exclude all reverse relations
37
  )
38
 
39
+ # ----------------- STEP 2: Create the FINAL Pydantic model for StockPriceData -----------------
40
+ # First, generate a base model from the ORM, excluding the default 'stock' relation
41
+ StockPriceData_Pydantic_Base = pydantic_model_creator(
42
+ StockPriceData,
43
+ name="StockPriceDataBase",
44
+ exclude=("stock",)
45
+ )
46
+
47
+ # Then, create a new class that inherits from the base and adds the 'stock' field back,
48
+ # but this time, its type is our new 'StockPydantic_Slim'.
49
+ class StockPriceData_Pydantic_Final(StockPriceData_Pydantic_Base):
50
+ stock: StockPydantic_Slim
51
+
52
+ # ----------------- STEP 3: Update the StockPriceData ORM model -----------------
53
  class StockPriceData(Model):
54
  id = fields.IntField(primary_key=True)
55
  stock = fields.ForeignKeyField("models.Stock", related_name="price_data")
 
66
 
67
  @staticmethod
68
  async def get_list(data: QuerySet):
69
+ """
70
+ Creates a Pydantic model for a list of StockPriceData objects.
71
+ This method now correctly uses our custom final Pydantic model to serialize each item.
72
+ """
73
+ # Create a list/queryset serializer using our final, correctly nested Pydantic model
 
 
 
 
74
  PriceDataListPydantic = pydantic_queryset_creator(
75
+ tortoise_model=StockPriceData,
76
+ pydantic_model=StockPriceData_Pydantic_Final, # This is the key change
77
+ name="PriceDataList"
78
  )
 
79
  return await PriceDataListPydantic.from_queryset(data)
80
 
81
  async def to_dict(self):
82
+ # This method is for single instances, not lists. It can stay as is.
83
  parser = pydantic_model_creator(StockPriceData)
84
  return await parser.from_tortoise_orm(self)
85
 
86
  class Meta:
87
  table = "stock_price_data"
88
  unique_together = (("stock", "date"),)
 
 
89
  class Dividend(Model):
90
  id = fields.IntField(primary_key=True)
91
  stock = fields.ForeignKeyField("models.Stock", related_name="dividends")