我觉得我要完成的查询不是太复杂,但是在尝试之后,我感觉有点迷茫。
我在测验应用程序中使用三种模型:
用户-具有“测验”属性,该属性与“测验”表有关系
测验-具有“问题”属性,该属性与“问题”表有关系。
问题-具有布尔值的“正确”属性。
对于我的查询,我想返回给定用户正确回答了多少个问题。我对SQLAlchemy的掌握并不特别,令人困惑的是模型关系链。
这是我的模特代码:
class Question(db.Model):
""" Question object that will be a part of a Quiz's question_list
Attributes:
key - word/item to be tested from subject dictionary
answer - correct answer generated via Quiz.type_method
definition - word definition from subject dict
question - question_string created by quiz.Quiz
"""
__tablename__ = 'questions'
question_id = db.Column('id', db.Integer, primary_key=True)
key = db.Column(db.String)
answer = db.Column(db.String)
definition = db.Column(db.String)
question = db.Column(db.String)
correct = db.Column(db.Boolean)
quiz_id = db.Column(db.Integer, db.ForeignKey('quizzes.id'))
quiz = db.relationship('Quiz', back_populates='questions')
def __repr__(self):
return (f"<Question(key='{self.key}', answer='{self.answer}', "
f"definition='{self.definition}', question='{self.question}', "
f"correct={self.correct}, quiz_id={self.quiz_id})>")
class Quiz(db.Model):
__tablename__ = 'quizzes'
quiz_id = db.Column('id', db.Integer, primary_key=True)
category = db.Column(db.String, nullable=False)
quiz_type = db.Column('type', db.String, nullable=False)
taken_on = db.Column(db.String,
default=datetime.utcnow().isoformat(' ', 'seconds'))
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
questions = db.relationship('Question', order_by=Question.question_id,
back_populates='quiz')
user = db.relationship('User', back_populates='quizzes')
@property
def num_correct(self):
return len([q for q in self.questions if q.correct])
@property
def num_wrong(self):
return len(self.questions) - self.num_correct
@property
def questions_asked(self):
return self.num_correct + self.num_wrong
@property
def questions_remaining(self):
return self.length - (self.num_correct + self.num_wrong)
@property
def score_percent(self):
return round(self.num_correct / self.questions_asked, 2)
def results(self):
""" Return formatted string summary of quiz results """
return 'You got {} question{} correct and {} question{} wrong.'.format(
self.num_correct, '' if self.num_correct == 1 else 's',
self.num_wrong, '' if self.num_wrong == 1 else 's')
# can use string formatting for title case
def __str__(self):
""" Return quiz_string for printing based on quiz_specs """
if self.quiz_type == 'definition':
return f'{self.category} {self.quiz_type} quiz'
else:
return f'{self.quiz_type} tense {self.category} quiz'
def __repr__(self):
return (f"<Quiz(category='{self.category}', "
f"quiz_type='{self.quiz_type}', quiz_id={self.quiz_id}, "
f"taken_on={self.taken_on})>")
# TODO: add admin column
class User(db.Model, UserMixin):
__tablename__ = 'users'
id = db.Column('id', db.Integer, primary_key=True)
username = db.Column(db.String, unique=True, nullable=False)
email = db.Column(db.String(120), index=True, unique=True)
password_hash = db.Column(db.String(128))
# db.String because sqlite has no DateTime type isoformat='YYYY-MM-DD'
created_on = db.Column(db.String,
default=datetime.utcnow().isoformat(' ', 'seconds'))
# num_correct , num_wrong
quizzes = db.relationship('Quiz', order_by=Quiz.quiz_id,
back_populates='user')
如果可以提供更好的上下文,请使用github链接:
我真的被困在这里。即使在正确方向上的一点也将受到极大的赞赏。