File size: 1,590 Bytes
9d4bd7c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
from tortoise import fields, models
from pydantic import BaseModel, ConfigDict
from tortoise.contrib.pydantic.creator import pydantic_model_creator, pydantic_queryset_creator
from tortoise.queryset import QuerySet
class UTTFund(models.Model):
id = fields.IntField(pk=True)
symbol = fields.CharField(max_length=20, unique=True)
name = fields.CharField(max_length=100)
@staticmethod
async def get_list(data):
if type(data) == type([UTTFund]):
parser=pydantic_queryset_creator(UTTFund)
return await parser.from_queryset(data)
async def to_dict(self):
if type(self) == UTTFund:
parser=pydantic_model_creator(UTTFund)
return await parser.from_tortoise_orm(self)
class UTTFundData(models.Model):
id = fields.IntField(pk=True)
fund = fields.ForeignKeyField("models.UTTFund", related_name="data")
date = fields.DateField()
nav_per_unit = fields.FloatField()
sale_price_per_unit = fields.FloatField()
repurchase_price_per_unit = fields.FloatField()
outstanding_number_of_units = fields.BigIntField()
net_asset_value = fields.BigIntField()
@classmethod
async def get_list(data):
if type(data) == QuerySet:
parser=pydantic_queryset_creator(UTTFundData)
return await parser.from_queryset(data)
async def to_dict(self):
if type(self) == models.Model:
parser=pydantic_model_creator(UTTFundData)
return await parser.from_tortoise_orm(self)
class Meta:
unique_together = ("fund", "date") |