Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 多对多django sql_Python_Sql_Django_Django Queryset - Fatal编程技术网

Python 多对多django sql

Python 多对多django sql,python,sql,django,django-queryset,Python,Sql,Django,Django Queryset,我有一个模型userprofile,它与另一个名为skill的表有很多关系 我还有一个模型组,它与另一个名为skill的表有很多关系 我想做一个查询,计算一下userprofile与每个组共有的技能数量 我还想做一个查询,将计数我的技能,一个特定的用户配置文件在cummon与其他用户配置文件 有人能帮我做这个吗。 多谢各位 模型: class UserProfile(models.Model): slug = models.SlugField(max_length=200)

我有一个模型userprofile,它与另一个名为skill的表有很多关系 我还有一个模型组,它与另一个名为skill的表有很多关系

我想做一个查询,计算一下userprofile与每个组共有的技能数量

我还想做一个查询,将计数我的技能,一个特定的用户配置文件在cummon与其他用户配置文件

有人能帮我做这个吗。 多谢各位

模型:

class UserProfile(models.Model):    
    slug = models.SlugField(max_length=200)
    user = models.ForeignKey(User, unique =True)
    skills = models.ManyToManyField(Skills,null=True, blank=True)
    courses = models.ManyToManyField(Course,null=True, blank=True)

class Group(models.Model):
    slug = models.SlugField(max_length=200)
    name = models.CharField(max_length=200)
    skills = models.ManyToManyField(Skills,null=True, blank=True)

class Skills(models.Model):
    slug = models.SlugField(max_length=200)
    name = models.CharField(max_length=200)
所以我找到了一种方法来帮助团队

groupRecommendation = Group.objects.extra(
select={
    'skills_count': """
     SELECT COUNT(*) FROM axiom_alto_userprofile_skills
     JOIN axiom_alto_group_skills on axiom_alto_userprofile_skills.skills_id = axiom_alto_group_skills.skills_id
     WHERE axiom_alto_group_skills.group_id= axiom_alto_group.id AND axiom_alto_userprofile_skills.userprofile_id = %d """ % profile.id,

},

).order_by('-skills_count')

但是我不知道如何在用户之间做到这一点

您的第一个查询是,对于特定的用户配置文件,计算用户与每个组共享的技能数量。在您的帖子中,您展示了一种使用子查询的方法。但是,在某些数据库中,子查询的性能比联接差得多,因此您可能有兴趣了解如何使用联接而不是子查询来获得相同的结果:

选择G.id、G.slug、G.name、COUNT(GS.id)作为技能计数
来自axiom_alto_群G
内部连接axiom_alto_userprofile_技能我们
在US.userprofile\u id=%s上
左外联接公理组
关于GS.group\u id=G.id和US.skills\u id=GS.skills\u id
按G.id、G.slug、G.name分组
按技能排序\u计数说明
在Django中,您可以在
模型上使用

Group.objects.raw(''' ... SQL as above ... ''', [profile.id])
UserProfile.objects.raw(''' ... SQL as above ... ''', [profile.id])
现在应该很容易看到如何执行第二个查询。也就是说,要计算特定用户配置文件中用户与其他用户共享的技能数量:

选择U.id、U.slug、U.user、COUNT(US2.id)作为U COUNT
来自axiom_alto_用户配置文件U
内部连接axiom_alto_userprofile_技能US1
在US1.userprofile\u id=%s上
左外连接axiom_alto_userprofile_技能US2
在US2.userprofile\U id=U.id和US2.skills\U id=US1.id上
按U.id、U.slug、U.user分组
按技能排序\u计数说明
同样,在Django中,您可以使用原始SQL查询运行该查询,这次是在
UserProfile
模型上:

Group.objects.raw(''' ... SQL as above ... ''', [profile.id])
UserProfile.objects.raw(''' ... SQL as above ... ''', [profile.id])

您的第一个查询是计算特定用户配置文件中用户与每个组共享的技能数量。在您的帖子中,您展示了一种使用子查询的方法。但是,在某些数据库中,子查询的性能比联接差得多,因此您可能有兴趣了解如何使用联接而不是子查询来获得相同的结果:

选择G.id、G.slug、G.name、COUNT(GS.id)作为技能计数
来自axiom_alto_群G
内部连接axiom_alto_userprofile_技能我们
在US.userprofile\u id=%s上
左外联接公理组
关于GS.group\u id=G.id和US.skills\u id=GS.skills\u id
按G.id、G.slug、G.name分组
按技能排序\u计数说明
在Django中,您可以在
模型上使用

Group.objects.raw(''' ... SQL as above ... ''', [profile.id])
UserProfile.objects.raw(''' ... SQL as above ... ''', [profile.id])
现在应该很容易看到如何执行第二个查询。也就是说,要计算特定用户配置文件中用户与其他用户共享的技能数量:

选择U.id、U.slug、U.user、COUNT(US2.id)作为U COUNT
来自axiom_alto_用户配置文件U
内部连接axiom_alto_userprofile_技能US1
在US1.userprofile\u id=%s上
左外连接axiom_alto_userprofile_技能US2
在US2.userprofile\U id=U.id和US2.skills\U id=US1.id上
按U.id、U.slug、U.user分组
按技能排序\u计数说明
同样,在Django中,您可以使用原始SQL查询运行该查询,这次是在
UserProfile
模型上:

Group.objects.raw(''' ... SQL as above ... ''', [profile.id])
UserProfile.objects.raw(''' ... SQL as above ... ''', [profile.id])

请共享模型。请共享模型。嗨,Gareth,谢谢,但问题是,skills\u count不是唯一的子查询,我有很多子查询可以获取其他计数,因此通过raw进行计算可能会更快,但要编写一个包含许多内部联接的sql。。。我不知道我是否能做到这将是一场噩梦。。。但你是对的,如果子查询花费太多时间,我可能最终会这样做。现在我在网站上没有那么多东西,但将来我会的,所以我可能需要重新考虑和使用raw。嗨,Gareth,谢谢,但问题是,skills\u count不是唯一的子查询,我有很多子查询可以获得其他计数,所以通过raw进行可能更快,但要编写一个包含很多内部连接的sql。。。我不知道我是否能做到这将是一场噩梦。。。但你是对的,如果子查询花费太多时间,我可能最终会这样做。现在我在网站上没有那么多东西,但将来我会的,所以我可能需要重新考虑并使用raw。