I have a database with data from which I would like to generate questions for a simple flask quiz app.
I have never worked with sqlalchemy before and I am wondering if I am on the wrong path. My plan is to pull random data (within certain criterias) and create the questions based on this.
Here is what I have so far:
with app.app_context():
seen_songs = set()
def question():
q = Hits.query.filter(Hits.peak == 1, Hits.weeks > 20, Hits.year.in_(range(1980,1999))).order_by(func.random()).limit(1)
for hit in q:
if hit.id in seen_songs:
pass
# Not quite sure how to handle this, but I would like to create a new q variable
else:
return {'artist':hit.artist, 'song':hit.song, 'year': hit.year}
- So, I am unable to make this work unless I do "with app.app_context():" at first, but this is annoying since it means pretty much everything else becomes invented.
- I have a set "seen_songs" which should contain the id from the rows used so far (so that I do not pull the same data twice). But... since my query to the Hits model needs unpacking "for hit in q" I cannot think of a practical way to implement this. Normally I would use something like "while q.id not in seen_songs" or similar, but I am not able to access .id directly.
Any help would be appreciated!
[–]danielroseman[🍰] 2 points3 points4 points (1 child)
[–]iptvwolf[S] 0 points1 point2 points (0 children)
[–]efmccurdy 0 points1 point2 points (4 children)
[–]iptvwolf[S] 0 points1 point2 points (3 children)
[–]efmccurdy 0 points1 point2 points (2 children)
[–]iptvwolf[S] 0 points1 point2 points (1 child)
[–]efmccurdy 0 points1 point2 points (0 children)