15

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'
2
  • why are you trying to create setters and getters at all, if they don't do anything? Commented Aug 10, 2015 at 9:08
  • As @maxfrie answer below is perfect answer for you, may you consider to select it as accepted @thomascrha? Commented Jul 12, 2018 at 9:01

1 Answer 1

39

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
Sign up to request clarification or add additional context in comments.

3 Comments

He doesn't need any getters/setters. email = Column(Text, nullable=False) is all he needs
@ThiefMaster it could be a contrived example. Anyway, useful answer setting me on the right track. Thanks.
Noticed 'validators' on that same linked page - exactly what i needed instead of a setter docs.sqlalchemy.org/en/14/orm/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.