Python-SQLAlchemy中,定义表对象时,何时应该写

 我来答
从空去听8
2017-11-21 · TA获得超过7440个赞
知道大有可为答主
回答量:6907
采纳率:93%
帮助的人:5592万
展开全部
描述问题
从Base继承的表对象, 何时写__init__? (标准是啥?)
我自己的观点是: 貌似写__init__完全没有必要
上下文环境
Python3
SQLAlchemy

Metadata-Version: 1.1Name: SQLAlchemyVersion: 1.0.11Summary: Database Abstraction Library

重现
相关代码
"""model.py

The datamodel, which represents Person that has multiple
Address objects, each with PostalCode, City, Country.

Person --(1..n)--> Address
Address --(has a)--> PostalCode
PostalCode --(has a)--> City
City --(has a)--> Country

"""from sqlalchemy import Column, Integer, String, ForeignKeyfrom sqlalchemy.orm import relationshipfrom .caching_query import FromCache, RelationshipCachefrom .environment import Base, bootstrapclass Country(Base):
__tablename__ = 'country'

id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False) def __init__(self, name):
self.name = nameclass City(Base):
__tablename__ = 'city'

id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
country_id = Column(Integer, ForeignKey('country.id'), nullable=False)
country = relationship(Country) def __init__(self, name, country):
self.name = name
self.country = countryclass PostalCode(Base):
__tablename__ = 'postal_code'

id = Column(Integer, primary_key=True)
code = Column(String(10), nullable=False)
city_id = Column(Integer, ForeignKey('city.id'), nullable=False)
city = relationship(City) @property
def country(self):
return self.city.country def __init__(self, code, city):
self.code = code
self.city = cityclass Address(Base):
__tablename__ = 'address'

id = Column(Integer, primary_key=True)
person_id = Column(Integer, ForeignKey('person.id'), nullable=False)
street = Column(String(200), nullable=False)
postal_code_id = Column(Integer, ForeignKey('postal_code.id'))
postal_code = relationship(PostalCode) @property
def city(self):
return self.postal_code.city @property
def country(self):
return self.postal_code.country def __str__(self):
return "%s\t"\ "%s, %s\t"\ "%s" % (self.street, self.city.name,
self.postal_code.code, self.country.name)class Person(Base):
__tablename__ = 'person'

id = Column(Integer, primary_key=True)
name = Column(String(100), nullable=False)
addresses = relationship(Address, collection_class=set) def __init__(self, name, *addresses):
self.name = name
self.addresses = set(addresses) def __str__(self):
return self.name def __repr__(self):
return "Person(name=%r)" % self.name def format_full(self):
return "\t".join([str(x) for x in [self] + list(self.addresses)])# Caching options. A set of three RelationshipCache options# which can be applied to Query(), causing the "lazy load"# of these attributes to be loaded from cache.cache_address_bits = RelationshipCache(PostalCode.city, "default").\
and_(
RelationshipCache(City.country, "default")
).and_(
RelationshipCache(Address.postal_code, "default")
)

bootstrap()

报错信息
相关截图
已经尝试哪些方法仍然没有解决(附上相关链接)
问题简化
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式