django m2m 如何在视图中获取 m2m 表元素

发布于 2024-09-06 01:02:20 字数 845 浏览 1 评论 0原文

我有一个使用 m2m 功能的模型:

class Classroom(models.Model):
     user = models.ForeignKey(User, related_name = 'classroom_creator')
     classname = models.CharField(max_length=140, unique = True)
     date = models.DateTimeField(auto_now=True)
     open_class = models.BooleanField(default=True)
     members = models.ManyToManyField(User,related_name="list of invited members", through = 'Membership')

我想在视图中获取一个类的所有成员并使用模板系统显示它们。 在视图中,我试图从这样的教室中获取所有成员:

        def inside_classroom(request,classname):

    try: 
         theclass = Classroom.objects.get(classname = classname)
         members = Members.objects.all()
    etc

但它不起作用,(尽管 db_table 被命名为 Classroom_Members)我想我必须使用另一个查询来获取教室中的所有成员类名。 另外,我想验证 request.user 是否是使用的成员(如果 request.user 在成员中)

我怎样才能 het 这些成员? 提前致谢!

i have a model using m2m feature:

class Classroom(models.Model):
     user = models.ForeignKey(User, related_name = 'classroom_creator')
     classname = models.CharField(max_length=140, unique = True)
     date = models.DateTimeField(auto_now=True)
     open_class = models.BooleanField(default=True)
     members = models.ManyToManyField(User,related_name="list of invited members", through = 'Membership')

and i want to take all members of one class in a view and display them using the template system.
In the view, i'm trying to take all the members from a classroom like that:

        def inside_classroom(request,classname):

    try: 
         theclass = Classroom.objects.get(classname = classname)
         members = Members.objects.all()
    etc

but it doesn't work,(though the db_table is named Classroom_Members) i guess i have to use another query for getting all the members from the classroom classname.
also, i want to verify if the request.user is a member using (if request.user in members)

how can i het those members?
Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

君勿笑 2024-09-13 01:02:20
theclass = Classroom.objects.get(classname = classname)
members = theclass.members.all()
theclass = Classroom.objects.get(classname = classname)
members = theclass.members.all()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文