I want to create two __init__
s inside the python class, that would instantiate different elements of that class.
The question was asked because I was trying to implement a partial update to a record inside the database and I was using SQLAlchemy ORM which maps python class and its attributes to database Table and column respectively. However, I was to update a user_score column in a User's table then I create an instance of the User object inside the update function that listens to the PATCH request but it wasn't updating. So, I thought I could create another init that will only be responsible for handling only that attribute user_score.
class User(db.Model):
__tablename__='users'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
score = Column(Integer, nullable=False)
def __init__(self, name, score):
self.name = name
self.score = score
def __init__(self, score):
self.score = score
def insert(self):
db.session.add(self)
db.session.commit()
def update(self):
db.session.commit()
def format(self):
return {
'id': self.id,
'name': self.name,
'score': self.score
}