The things between SQLAlchemy and Flask-sqlalchemy are not too clear.
SQLAlchemy documentation too damn much, day.
The instance that was encountered today.
The user randomly matches, and the system randomly returns a user under certain filter conditions. For the user experience, you have to get rid of the people you already care about.
First get a list of users that meet the filter criteria:
Users = User.query.filter_by (School=form.school.data, Sex=form.sex.data, Status=form.status.data). All ()
And to get a list of people you've followed:
followed = G.user.followed.all ()
The point is, how do you subtract the other list from the first one? Without this method, the time complexity should be super large with a for loop and if judgment.
Therefore, you should first list to set and then set to find the difference set and then go to the list.
Filter_users = List (set (users)-set (followed)) # Lists to set to find the difference set and then to go to the list
Then we are going to start randomly selecting a user. Find the documentation for the Python random module and find that choice () is a good, random selection in the sequence.
Note that if it is an empty sequence, it will be an error.
Therefore, the following code is:
If filter_users: user = Random.choice (filter_users) Flash (U ' you match to the following users: ') return redirect (' Profile ', id=user.id)] else: Flash (U ' ah oh, no user satisfies this condition ') # later use the mascot to express, do not use Flash return render_template ( ' match.html ', form=form)
Above.
Today learned a lot of things, what hash, data structure, time complexity are out.
Cock.
The database filters the user, then removes a portion (the list is poor) and then randomly returns a user. SQLAlchemy + Python Collection (set) + random