How can one store and retrieve a python object in a database with sqlalchemy?
I've been looking at the documentation but don't see anything where they are storing more than dictionaries. I've been told SQLalchemy has the capability to store a python object but am yet to see it. Anyone know an example of how this is done?
Below is an example of how I thought it could work, but I don't believe it's possible to add a python object as a Column.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
# Create our database model
class User(db.Model):
""" User Model for python objects to a user name """
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(255), unique=True, nullable=False)
def __init__(self, name):
self.name = name
# create function to add python object
def add_python_object(self, object_to_store):
persistent_python_object = db.Column() # <-- would like to add python object here
self.persistent_python_object = object_to_store
# Define our object we would like to store
class ExampleObject(object):
def __init__(self, val_1, val_2):
self.val_1 = val_1
self.val_2 = val_2
def does_something_from_storage(self):
return self.val_1 + self.val_2
# Create user
adder = User('Adder')
adder.add_python_object(ExampleObject(3,4))
# Add to database
db.session.add(adder)
db.session.commit()
# Retrieve python object
user = User.query.filter_by(name='Adder').first()
result = user.persistent_python_object.does_something_from_storage()
As @Andrew Allen mentioned, SQLAlchemy actually comes with a pickleType column which can be used to serialise python objects.
from flask import Flask
from flask_bcrypt import Bcrypt
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
db = SQLAlchemy(app)
db.create_all()
# Create our database modl
class User(db.Model):
""" User Model for storing user related details """
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
name = db.Column(db.String(255), unique=True, nullable=False)
python_object = db.Column(db.PickleType(), nullable=True)
def __init__(self, name):
self.name = name
# create funciton to add python object
def add_python_object(self, object_to_store):
persistent_python_object = db.Column() # <-- would like to add python object here
self.persistent_python_object = object_to_store
# Define our object we would like to store
class ExampleObject(object):
def __init__(self, val_1, val_2):
self.val_1 = val_1
self.val_2 = val_2
def does_something_from_storage(self):
return self.val_1 + self.val_2
# Create user
adder = User('Adder')
adder.add_python_object(ExampleObject(3,4))
# Add to database
db.session.add(adder)
db.session.commit()
# Retrieve python object
user = User.query.filter_by(name='Adder').first()
result = user.persistent_python_object.does_something_from_storage()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With