Looks like you could define a relation between user and answer that fills that
However, if you you want to select a set of user answers that correspond to certain
types of quizzes etc then you should use a query with joins. The relationship
attributes are just shorthands for simople foriegn key lookups. Any complex query that
depends on mulitple columns in multiple tables should probably be formulated as a
An alternative approach (which is effectively the same as a for loopl but more
pythonic) would be to use quiz.question.user_answers in a list comprehension.
"Nicholas Turner" <21*1*7*[email protected]*u*e*t*u*a*e*u*a*> wrote:
> Hi there,
> I was looking for a while and I'm having trouble seeing how this is possible and just > put a hacky for loop in instead. Say I have 3 models
> Quiz:> questions = db.relationship('Question', backref='quiz', lazy='dynamic')
> Question:> user_answers = db.relationship('UserAnswer', back_populates="answered_question")
> UserAnswer:> user_id = db.Column(db.Integer, ForeignKey('user.id'))> timestamp = db.Column(db.DateTime, default = datetime.utcnow)> answered_question = db.relationship('Question', back_populates="user_answers")
> Is there a way I can do this without query using only objects?> Like:> userAnswers = quiz.questions.user_answers.filter_by(user_id=current_user)
> Do I have to make a join?