我正在尝试建立一个单一课程的讲台,在该讲台上,我只会持有只有会员才能看到的课程单元资料,但是当我尝试检索Lesson.course_allowed_mem_types.all()
时,出现以下错误'ManyToManyDescriptor' object has no attribute 'all'
,我该如何解决这个简单的错误?
class Lesson(models.Model):
content_title = models.CharField(max_length=120)
content_text = models.CharField(max_length=200)
thumbnail = models.ImageField(upload_to='static/xxx/xxx/xxx/xxx')
link = models.CharField(max_length=200, null=True)
allowed_memberships = models.ManyToManyField(Membership)
def __str__(self):
return self.content_title
观看次数
def get_context_data(self, **kwargs):
context = super(bootCamp, self).get_context_data(**kwargs)
context['lessons'] = Lesson.objects.all()
user_membership = UserMembership.objects.filter(user=self.request.user).first()
user_membership_type = user_membership.membership.membership_type
course_allowed_mem_types = Lesson.allowed_memberships.all()
context['course_allowed_mem_types'] = course_allowed_mem_types
return context
lesson = Lessons.objects.filter(pk=1)
course_allowed_mem_types = lesson.allowed_memberships.all()
Lesson.allowed_memberships.all()
)到底是什么。是“与现有课程对象相关的所有成员对象”
,还是“可与课程对象相关的所有成员对象”?这些是不同的查询,并且Lesson.allowed_memberships.all()
也没有暗示,这是不正确的用法。
Membership.objects.filter(lesson__in=Lesson.objects.all())
((您已经将其作为context['lessons']
,因此请改用它,仅显示其主意)