通过SqlAlchemy模型类定义中的@property进行Python getter和setter:HOWTO [英] Python getter and setter via @property within SqlAlchemy model class definition: HOWTO
问题描述
因此,我对sqlalchemy和ORM非常陌生.我有一个现有的数据库postgresql,并且已经创建了一个与数据库进行通信的模型.下面是我的Transcribers表的类.通过它进行查询时,所有这些都可以工作.我在课程中设置获取器和设置器时遇到问题.
So I am very new to sqlalchemy and ORM. I have an existing database, postgresql, and I have created a model to communicate to the database. Below is the class for my Transcribers table. All of whcih works when querying via it. I am just having problems with setting up getters and setters within the class.
class Transcriber(Base):
__tablename__ = 'transcribers'
__table_args__ = (
UniqueConstraint('projectid', 'email'),
)
transcriberid = Column(Integer, primary_key=True, server_default=text("nextval('transcribers_transcriberid_seq'::regclass)"))
projectid = Column(ForeignKey(u'projects.projectid', ondelete=u'CASCADE'), index=True)
email = Column(Text, nullable=False)
created = Column(DateTime, nullable=False, server_default=text("now()"))
onwebsite = Column(Boolean, nullable=False, server_default=text("true"))
def __repr__(self):
return "<Transcriber(transcriberid:'%s', projectID:'%s', email:'%s', created:'%s', onwebsite:'%s'" \
%(self.transcriberid, self.projectid, self.email, self.created, self.onwebsite)
@property
def transcriberid(self):
return self.transcriberid
def email(self):
return self.email
@email.setter
def email(self, value):
self.email = value
project = relationship(u'Project')
我不确定如何使用@property方法访问对象内的不同变量.我想使用这种方法,因为我相信它更具Python感.
I am not sure how to use the @property method to access different variables within the object. I want to use this methodology as I believe its more pythonic.
所以现在我该如何实际调用这些方法.并且它们是否正确设置.
So now how do I actually call these methods. And are they correctly set up.
启动课程时,我收到此错误
I recieve this error when starting the Class
Traceback (most recent call last):
File "./test.py", line 4, in <module>
from peraAPI import DBSession, getProjectbyId, getTransById
File "/Users/tcrha/bin/working/PeraPera/peraAPI/__init__.py", line 7, in <module>
from model_local import Project, Transcriber
File "/Users/tcrha/bin/working/PeraPera/peraAPI/model_local.py", line 110, in <module>
class Transcriber(Base):
File "/Users/tcrha/bin/working/PeraPera/peraAPI/model_local.py", line 133, in Transcriber
@email.setter
AttributeError: 'function' object has no attribute 'setter'
推荐答案
You can use hybrid_property. In that case simplified version of your code should look like:
from sqlalchemy.ext.hybrid import hybrid_property
class Transcriber(Base):
__tablename__ = 'transcribers'
__table_args__ = (
UniqueConstraint('projectid', 'email'),
)
transcriberid = Column(Integer, primary_key=True, server_default=text("nextval('transcribers_transcriberid_seq'::regclass)"))
projectid = Column(ForeignKey(u'projects.projectid', ondelete=u'CASCADE'), index=True)
created = Column(DateTime, nullable=False, server_default=text("now()"))
onwebsite = Column(Boolean, nullable=False, server_default=text("true"))
_email = Column('email', Text, nullable=False)
@hybrid_property
def email(self):
return self._email
@email.setter
def email(self, email):
self._email = email
这篇关于通过SqlAlchemy模型类定义中的@property进行Python getter和setter:HOWTO的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!