I have a Flask application that is using SQLite database. I have created this model to load data into the db
class Category(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
products = db.Column(db.Text)
This database has been populated with a lot of information, and I am trying to change a specific variable in a dictionary within the stored information.
# print 'products' for the first Category stored
x = Category.query.all()
xLoaded = json.loads(x[0].products)
print(xLoaded)
# output
[['test1', {'path': ['a', 'b', 'c']}], ['test2', {'path': ['d', 'e', 'f']}]]
Specifically, I am trying to change 'b'
in 'test1'
so I tried this:
x = Category.query.all()
xLoaded = json.loads(x[0].products)
xLoaded[0][1]['path'][1] = 'z'
db.session.commit()
However, this fails to change anything.
CodePudding user response:
The code only changes the value of the variable xLoaded
not the x
.
Change the value of variable x
x = Category.query.all()
xLoaded = json.loads(x[0].products)
xLoaded[0][1]['path'][1] = 'z'
x[0].products = xLoaded
db.session.commit()