开发者

django m2m how can i get m2m table elements in a view

开发者 https://www.devze.com 2023-01-04 07:12 出处:网络
i have a model using m2m feature: class Classroom(models.Model): user = models.ForeignKey(User, related_name = \'classroom_creator\')

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!


theclass = Classroom.objects.get(classname = classname)
members = theclass.members.all()
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号