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

10,807

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
Share:
10,807
thomascrha
Author by

thomascrha

Updated on June 27, 2022

Comments

  • thomascrha
    thomascrha almost 2 years

    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')
    

    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'
    
  • ThiefMaster
    ThiefMaster almost 5 years
    He doesn't need any getters/setters. email = Column(Text, nullable=False) is all he needs
  • Jérôme
    Jérôme over 2 years
    @ThiefMaster it could be a contrived example. Anyway, useful answer setting me on the right track. Thanks.