2015-02-10 42 views
0

我將以文檔中的示例來解釋它。使用ManyToManyField時出現孤行?

class Student(BaseModel): 
    name = CharField() 

class Course(BaseModel): 
    name = CharField() 
    students = ManyToManyField(Student, related_name='courses') 

StudentCourse = Course.students.get_through_model() 

在某些時候,我想完全從數據庫中刪除一個學生。這在現在分兩步進行:

student.courses.clear() 
student.delete_instance() 

這將通過表中刪除在StudentCourse該行預期,並在Course表中的行仍然存在。如果其他學生仍然使用這門課程,那就夠了。但是如果我想要刪除課程,如果這個學生是唯一使用它的人呢?

我是否需要一些額外的邏輯來實現這個功能,或者應該讓peewee以某種方式處理這個問題?在這一點上我有點困惑,因爲ManyToManyField是如此的新,所以沒有太多的外部信息。

回答

0

如果您決定在清除所有關聯的學生時刪除課程,則必須明確地處理該問題。