Skip to content Skip to sidebar Skip to footer

Changes To Model Class's __init__ Method Don't Seem To Be Taking Effect

I have two particular model classes that are giving me errors during my testing, upon inspecting the methods for each I was pretty certain my issues were the result of typos on my

Solution 1:

A better way to override __init__ when using flask-sqlalchemy is to use reconstructor. Object initialization with sqlalchemy is a little tricky, and flask-sqlalchemy might be complicating it as well.. anyways, here's how we do it:

from sqlalchemy.orm import reconstructor

class MyModel(db.Model):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.init_on_load()

    @reconstructor
    def init_on_load(self):
        # put your init stuff here

Post a Comment for "Changes To Model Class's __init__ Method Don't Seem To Be Taking Effect"