Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'
like image 869
thomascrha Avatar asked Aug 10 '15 03:08

thomascrha


1 Answers

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
like image 186
maxfrei Avatar answered Nov 24 '22 08:11

maxfrei