如何查询多对多关系? [英] how to query a many to many relationship?
问题描述
我正在建立一个电子学习平台
我有用户(Utilisateur)谁可以采取几个课程,每个课程都有几个模块,一个模块可以在几个课程中
一个用户可以作为学生或教师或管理员,学生由教师或几位管理,老师也可以由老师(如果他是研究员)管理,教师由管理员管理
我不熟悉many_to_many概念,请更正我
这是我的django模型:
class Utilisateur(models.Model):
user = models.OneToOneField(User)
role = models.CharField(choices = ROLES,blank = False,max_length = 50)
managed_by = models.ManyToManyField('self',
related_name ='managers',
symmetry = False,
blank = True)
course = models.ManyToManyField('Course')#can可以做吗? (问题2)
module = models.ManyToManyField('Module',through ='UtilisateurModule')
class课程(models.Model):
titre = model.CharField(blank = False,max_length = 100)
类模块(models.Model):
titre = models.CharField(blank = False,max_length = 100)
$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 ForeignKey(Course)
order = models.IntegerField(blank = False)
class UtilisateurModule(models.Model):
user = models.ForeignKey(Utilisateur)
module = models.ForeignKey(Module)
score = models.IntegerField(blank = False)
我的问题:
1 。如何通过模块获取用户的分数,并按以下方式显示:
- A
*模块1
分数:5
*模块2:
分数:8
。 ..
-Course B
*模块1:
分数7
....
2 。我需要在课程和用户之间添加许多关系,因为我不会告诉哪个课程影响到哪个用户,只有通过影响一个模块才知道该模块可以属于多个课程,是否正确添加一个 course = models.ManyToManyField('Course')
在Utilisateur?
SOLVED
1-这是我如何设法完成的:
course = Utilisateur.objects.get(user = current_user).course.all()
course_sets = []
当然是p
modules = Module.objects.filter (course__id = p.pk).order_by('modulecourse__order')
modules_sets = []
在模块中的m:
score = UtilisateurModule.objects.get(module = m,utilisateur = current_user )
modules_sets.append((m,score))
course_sets.append((p,modules_sets))
2-我补充说 course = models.ManyToManyField('Course')
to Utilisateur
,然后添加一些手动验证,每当A课程受到用户影响时,该课程中的模块也会受到 UtilisateurModule
I'm trying to build an e-learning platform
I have users (Utilisateur) who can take several courses ,each course have several modules and a module can be in several courses
A user can be a student or teacher or admin, a student is "managed" by a teacher or several and a teacher can also be managed by a teacher (if he is a researcher) ,teachers are managed by admins
I'm not familiar with many_to_many concept, please correct me
This is my django models :
class Utilisateur(models.Model):
user = models.OneToOneField(User)
role = models.CharField(choices=ROLES,blank=False,max_length=50)
managed_by = models.ManyToManyField('self',
related_name='managers',
symmetrical=False,
blank=True)
course = models.ManyToManyField('Course') #can I do it? (question 2)
module = models.ManyToManyField('Module', through='UtilisateurModule')
class Course(models.Model):
titre = models.CharField(blank=False,max_length=100)
class Module(models.Model):
titre = models.CharField(blank=False,max_length=100)
course = models.ManyToManyField(Course, through='ModuleCourse')
class ModuleCourse (models.Model):
module = models.ForeignKey(Module)
course = models.ForeignKey(Course)
order = models.IntegerField(blank=False)
class UtilisateurModule (models.Model):
user = models.ForeignKey(Utilisateur)
module = models.ForeignKey(Module)
score = models.IntegerField(blank=False)
My questions :
1. How to get the scores of a user by module and display them by course, like that :
-Course A
*Module 1
score : 5
*module 2 :
score : 8
...
-Course B
*Module 1:
score 7
....
2. I need to add a many to many relationship between course and user because I wouldnt tell which course affected to which user only by affecting a module to him knowing that the module can belong to several courses, is it correct to add a course = models.ManyToManyField('Course')
in Utilisateur?
SOLVED
1- This is how I've managed to do it :
course = Utilisateur.objects.get(user=current_user).course.all()
course_sets = []
for p in course:
modules = Module.objects.filter(course__id=p.pk).order_by('modulecourse__order')
modules_sets = []
for m in modules:
score = UtilisateurModule.objects.get(module=m,utilisateur=current_user)
modules_sets.append((m, score))
course_sets.append((p, modules_sets))
2- I added course = models.ManyToManyField('Course')
to Utilisateur
and then added some manual verification that whenever A course is affected to a user the modules in that course are also affected to the same user in UtilisateurModule
这篇关于如何查询多对多关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!