将模型的一个属性写入数据库,然后从这个单一属性中读取整个模型
Write one of model's attributes to the database and then read entire model from this single attribute
我正在尝试创建一个 Currency
模型来包装 pycountry
的 Currency
对象。该模型将包括货币代码和全名。但是,我只想在调用模型的 .dict()
方法时将代码存储在我的数据库中。然后从数据库中读取此代码时,它应该使用 pycountry
检索完整的货币对象并将货币名称也存储在模型中。
import pycountry
from pydantic import BaseModel
class Currency(BaseModel):
code: str
name: str
def __init__(self, code: str):
currency = pycountry.currencies.get(alpha_3=code)
super().__init__(code=currency.alpha_3, name=currency.name)
def dict(self, **kwargs):
return self.code
class Country(BaseModel):
name: str
currency: Currency
country = Country(name="United States of America", currency=Currency("USD"))
print(country)
这会打印:
name='United States of America' currency=Currency(code='USD', name='US Dollar')
现在我使用 country.dict()
写入我的 MongoDB 数据库。
这看起来像:
name | currency
-------------------------------------
'United States of America' | 'USD'
现在,当我从数据库中读取此内容时,我希望对象与我之前打印时相同,currency
填充为 Currency(code='USD', name='US Dollar')
,但是当我读取此内容时 Country
从数据库中,我得到 value is not a valid dict (type=type_error.dict)
.
我怎样才能做到这一点?
这是你想要的结果吗?
`import pycountry
from pydantic import BaseModel
class Currency(BaseModel):
code: str
name: str
def __init__(self, code: str):
currency = pycountry.currencies.get(alpha_3=code)
super().__init__(code=currency.alpha_3, name=currency.name)
class Country(BaseModel):
name: str
currency: Currency
country = Country(name="United States of America", currency=Currency("USD"))
print(country)
print(country.dict())
name='United States of America' currency=Currency(code='USD', name='US Dollar')
{'name': 'United States of America', 'currency': {'code': 'USD', 'name': 'US Dollar'}}
我正在尝试创建一个 Currency
模型来包装 pycountry
的 Currency
对象。该模型将包括货币代码和全名。但是,我只想在调用模型的 .dict()
方法时将代码存储在我的数据库中。然后从数据库中读取此代码时,它应该使用 pycountry
检索完整的货币对象并将货币名称也存储在模型中。
import pycountry
from pydantic import BaseModel
class Currency(BaseModel):
code: str
name: str
def __init__(self, code: str):
currency = pycountry.currencies.get(alpha_3=code)
super().__init__(code=currency.alpha_3, name=currency.name)
def dict(self, **kwargs):
return self.code
class Country(BaseModel):
name: str
currency: Currency
country = Country(name="United States of America", currency=Currency("USD"))
print(country)
这会打印:
name='United States of America' currency=Currency(code='USD', name='US Dollar')
现在我使用 country.dict()
写入我的 MongoDB 数据库。
这看起来像:
name | currency
-------------------------------------
'United States of America' | 'USD'
现在,当我从数据库中读取此内容时,我希望对象与我之前打印时相同,currency
填充为 Currency(code='USD', name='US Dollar')
,但是当我读取此内容时 Country
从数据库中,我得到 value is not a valid dict (type=type_error.dict)
.
我怎样才能做到这一点?
这是你想要的结果吗?
`import pycountry
from pydantic import BaseModel
class Currency(BaseModel):
code: str
name: str
def __init__(self, code: str):
currency = pycountry.currencies.get(alpha_3=code)
super().__init__(code=currency.alpha_3, name=currency.name)
class Country(BaseModel):
name: str
currency: Currency
country = Country(name="United States of America", currency=Currency("USD"))
print(country)
print(country.dict())
name='United States of America' currency=Currency(code='USD', name='US Dollar')
{'name': 'United States of America', 'currency': {'code': 'USD', 'name': 'US Dollar'}}