Python getter and setter via @property in SqlAlchemy model class definition: HOWTO

So, I am very new to sqlalchemy and ORM. I have an existing database, postgresql, and I created a model to communicate with the database. Below is the class for the Transcribers table. Anything that works when requested through it. I'm just having trouble setting getters and setters inside 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')

      

I'm not sure how to use the @property method to access various variables within an object. I want to use this methodology as I find it more pythonic.

So how do I actually call these methods. And they are configured correctly.

I am getting this error when running 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'

      

+3


source to share


1 answer


You can use hybrid_property . In this case, a simplified version of your code should look like this:



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

      

+1


source







All Articles