如何从课程模型中修复ManyToManyDescriptor-视图?

问题描述 投票:1回答:3

我正在尝试建立一个单一课程的讲台,在该讲台上,我只会持有只有会员才能看到的课程单元资料,但是当我尝试检索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
python django many-to-many
3个回答
0
投票
[尝试选择一个课程然后检索关联的成员

lesson = Lessons.objects.filter(pk=1)course_allowed_mem_types = lesson.allowed_memberships.all()


0
投票
“ Lesson类的所有具体允许的成员资格对象”(Lesson.allowed_memberships.all())到底是什么。

“与现有课程对象相关的所有成员对象”

,还是

“可与课程对象相关的所有成员对象”?这些是不同的查询,并且Lesson.allowed_memberships.all()也没有暗示,这是不正确的用法。

如果您要使用前者,可以使用类似的方法

Membership.objects.filter(lesson__in=Lesson.objects.all())

((您已经将其作为context['lessons'],因此请改用它,仅显示其主意)


0
投票
© www.soinside.com 2019 - 2024. All rights reserved.