django m2m 如何在视图中获取 m2m 表元素
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)