我遇到了一个问题,涉及到Course
和LearnerCourse
两个模型。模型定义如下:
class Course(BaseModel, Timestamps, SoftDelete):
name = models.TextField(_("Course Name"), max_length=100, blank=True, null=True, unique=False)
description = models.TextField(_("Course Description"), blank=True, null=True)
course_number = models.CharField(max_length=15, null=True, blank=True, unique=True)....
class LearnerCourse(BaseModel, Timestamps, SoftDelete):
course = models.ForeignKey(Course, verbose_name=_("Course"), on_delete=models.CASCADE, db_index=True, related_name='course_learner_courses')
learner = models.ForeignKey(Learner, verbose_name=_("Learner Course"), on_delete=models.CASCADE, db_index=True)
course_subscription = models.ForeignKey(CourseSubscription, null=True, on_delete=models.CASCADE, db_index=True)
enroll_date = models.DateTimeField(_("Course Enroll Date"))
is_favourite = models.BooleanField(default=False)
在LearnerCourse
模型中,我对course
字段使用了related_name='course_learner_courses'
。然后,当我尝试这样使用它们时:
details = LearnerCourse.objects.filter(learner=learner_id).all().order_by('-datetime_created')
print("details", details)
courses = details.course_learner_courses.all()
print("courses", courses)
我遇到了一个错误,提示 'SafeDeleteQueryset' object has no attribute 'course_learner_courses'
。请给我一些建议来解决这个问题。