Reputation: 151
Really new sqlalchemy user here with what is probably a basic question, but for the life of me I can't figure out the proper way to query my data. Here is my class.
class Reading(db.Model):
__tablename__ = 'reading'
rid = db.Column(db.Integer, primary_key=True)
uid = db.Column(db.Integer, db.ForeignKey('user.uid'))
sid = db.Column(db.Integer, db.ForeignKey('sensor.sid'))
readingTimestamp = db.Column(db.DateTime())
readingLightValue = db.Column(db.Integer)
readingLightStatus = db.Column(db.String(6))
readingTemp1 = db.Column(db.Float)
readingTemp2 = db.Column(db.Float)
readingHumidity = db.Column(db.Float)
Writing data is working fine, now if I try to create a query like so.
result = db.session.query(Reading).order_by(Reading.readingTimestamp.desc()).limit(50)
I get a query object as you would expect,
In [36]: result.all()
Out[36]:
[<app.account.models.Reading at 0x2b85ad0>,
<app.account.models.Reading at 0x2b85490>,
<app.account.models.Reading at 0x2b85250>,
but I can't figure out how to access attributes of the model. I want to take these readings and render them out back as a table.
Thanks for setting me straight! :)
Upvotes: 0
Views: 128
Reputation: 19319
It's a list and the attributes become the attributes of the object. You'd probably want to do something like this:
for res in result.all():
# do something with the id
do_something(res.id)
Each res
is an instance of your model.
Upvotes: 1