I'm trying to create a mechanism of duplicating sqlalchemy models in flask. To do so, I'm running the following logic:
class Model:
primitive_type = (int, bool, float, str)
def copy(self):
copy = type(self)()
for key, val in self.__dict__.items():
if not key.startswith('_') and isinstance(val, Model.primitive_type):
copy.__setattr__(key, val)
return copy
Then I've a model named sensor which is an abstract model:
class Sensor(db.Model):
__tablename__ = 'sensor'
id = db.Column(db.Integer, primary_key=True)
device_id = db.Column(db.Integer, db.ForeignKey('device.id'))
type = db.Column(db.String(128), nullable=False)
__mapper_args__ = {
'polymorphic_identity': 'sensor',
'polymorphic_on': type
}
And finally I've the sensor itself which ineriths both from Model and Sensor:
class Pnt(Model, Sensor):
__tablename__ = 'pnt'
id = db.Column(db.Integer, db.ForeignKey('sensor.id'), primary_key=True)
host_ip = db.Column(db.String(100), nullable=False, info={'label': "Ip"})
...
def copy(self):
copy = super().copy()
return copy
The problem is that super().copy() returns the values from Sensor and not from Pnt. This method isn't working only in this case. Any help would be appreciated.