👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I"m trying to jsonify a SQLAlchemy result set in Flask/Python.
The Flask mailing list suggested the following method http://librelist.com/browser//flask/2011/2/16/jsonify-sqlalchemy-pagination-collection-result/#04a0754b63387f87e59dda564bde426e :
return jsonify(json_list = qryresult)
However I"m getting the following error back:
TypeError: <flaskext.sqlalchemy.BaseQuery object at 0x102c2df90>
is not JSON serializable
What am I overlooking here?
I have found this question: How to serialize SqlAlchemy result to JSON? which seems very similar however I didn"t know whether Flask had some magic to make it easier as the mailing list post suggested.
Edit: for clarification, this is what my model looks like
class Rating(db.Model):
__tablename__ = "rating"
id = db.Column(db.Integer, primary_key=True)
fullurl = db.Column(db.String())
url = db.Column(db.String())
comments = db.Column(db.Text)
overall = db.Column(db.Integer)
shipping = db.Column(db.Integer)
cost = db.Column(db.Integer)
honesty = db.Column(db.Integer)
communication = db.Column(db.Integer)
name = db.Column(db.String())
ipaddr = db.Column(db.String())
date = db.Column(db.String())
def __init__(self, fullurl, url, comments, overall, shipping, cost, honesty, communication, name, ipaddr, date):
self.fullurl = fullurl
self.url = url
self.comments = comments
self.overall = overall
self.shipping = shipping
self.cost = cost
self.honesty = honesty
self.communication = communication
self.name = name
self.ipaddr = ipaddr
self.date = date
👻 Read also: what is the best laptop for engineering students?
jsonify a SQLAlchemy result set in Flask __del__: Questions
How can I make a time delay in Python?
5 answers
I would like to know how to put a time delay in a Python script.
Answer #1
import time
time.sleep(5) # Delays for 5 seconds. You can also use a float value.
Here is another example where something is run approximately once a minute:
import time
while True:
print("This prints once a minute.")
time.sleep(60) # Delay for 1 minute (60 seconds).
Answer #2
You can use the sleep()
function in the time
module. It can take a float argument for sub-second resolution.
from time import sleep
sleep(0.1) # Time in seconds
jsonify a SQLAlchemy result set in Flask __del__: Questions
How to delete a file or folder in Python?
5 answers
How do I delete a file or folder in Python?
Answer #1
os.remove()
removes a file.os.rmdir()
removes an empty directory.shutil.rmtree()
deletes a directory and all its contents.
Path
objects from the Python 3.4+ pathlib
module also expose these instance methods:
pathlib.Path.unlink()
removes a file or symbolic link.pathlib.Path.rmdir()
removes an empty directory.
We hope this article has helped you to resolve the problem. Apart from jsonify a SQLAlchemy result set in Flask, check other __del__-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano jsonify a SQLAlchemy result set in Flask
- Deutsch jsonify a SQLAlchemy result set in Flask
- Français jsonify a SQLAlchemy result set in Flask
- Español jsonify a SQLAlchemy result set in Flask
- Türk jsonify a SQLAlchemy result set in Flask
- Русский jsonify a SQLAlchemy result set in Flask
- Português jsonify a SQLAlchemy result set in Flask
- Polski jsonify a SQLAlchemy result set in Flask
- Nederlandse jsonify a SQLAlchemy result set in Flask
- 中文 jsonify a SQLAlchemy result set in Flask
- 한국어 jsonify a SQLAlchemy result set in Flask
- 日本語 jsonify a SQLAlchemy result set in Flask
- हिन्दी jsonify a SQLAlchemy result set in Flask
Munchen | 2023-03-24
Thanks for explaining! I was stuck with jsonify a SQLAlchemy result set in Flask for some hours, finally got it done 🤗. Checked yesterday, it works!
Abu Dhabi | 2023-03-24
Maybe there are another answers? What jsonify a SQLAlchemy result set in Flask exactly means?. Checked yesterday, it works!
Singapore | 2023-03-24
Maybe there are another answers? What jsonify a SQLAlchemy result set in Flask exactly means?. Checked yesterday, it works!