2016-11-05 64 views
1

我是新来的ponyorm。如何在与ponyorm多对多的情况下选择记录

假设我有其中这两个类和许多一对多的关系:

class Student(db.Entity): 
    id = PrimaryKey(str) 
    name = Required(str) 
    courses = Set("Course") 

class Course(db.Entity): 
    id = PrimaryKey(str) 
    name = Required(str) 
    semester = Required(int) 
    students = Set(Student) 

我想选择一些课程,之后是一个特殊的学生。我要做的就是:

student = Student.select(lambda s: s.id == id).get() 
courses = Course.select(lambda c: c.students == student).get() 

而且我得到这个错误:

Incomparable types 'Set of Student' and 'Student' in expression: c.students == student 

什么是做到这一点的正确方法是什么? 谢谢

回答

1

我不知道确切的库,但我认为问题是,c.students指定所有学生的集合,因此测试平等就像没有太多的意义。

您可能希望你的第二个行改变这样的事情(我没有,虽然测试):

Course.select(lambda c: student in c.students).get() 

这让我想知道如果确实有更好的方法来做到这一点。如果你正在尝试的是检索课程,一个特定的学生参加为什么你不只是从变量student检索courses字段?

喜欢的东西

student = Student.select(lambda s: s.id == id).get() 
student.courses # do something with it 
+0

你是对的!我可以遍历'student.courses'并获得我需要的! – gaetano

相关问题