简体   繁体   English

多对多Django SQL

[英]many to many django sql

I have a model userprofile that as a manytomany relationship with another table called skill I also have a model group that as a manytomany relationship with another table called skill 我有一个模型用户配置文件,它与另一个与技能表的多对多关系也与一个模型组

I would like to make a query that will count me the number of skills that the userprofile have in common with each group. 我想进行查询,以计算出我的个人资料与每个小组共有的技能数量。

I also would like the make a query that will count me the numbers of skills that a specific userprofile has in cummon with others user profile 我还想进行查询,以计算出特定用户个人资料与其他用户个人资料在召唤中的技能数量

Could someone help me to do this. 有人可以帮我做到这一点。 Thank you 谢谢

the models : 型号:

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)

So I found a way to do it for groups 所以我找到了一种针对团体的方法

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')

But I don't know how to do it between users 但是我不知道如何在用户之间进行操作

Your first query is to count, for a specific user profile, the number of skills that user shares with each group. 您的第一个查询是针对特定的用户个人资料统计用户与每个组共享的技能数量。 In your post, you show a way to do it using a subquery. 在您的文章中,您展示了一种使用子查询的方法。 However, in some databases subqueries have much poorer performance than joins, so you might be interested to see how to do get the same results using a join instead of the subquery: 但是,在某些数据库中,子查询的性能要比联接差很多,因此您可能有兴趣了解如何使用联接而不是子查询来获得相同的结果:

SELECT G.id, G.slug, G.name, COUNT(GS.id) AS skills_count
FROM axiom_alto_group G
INNER JOIN axiom_alto_userprofile_skills US
  ON US.userprofile_id = %s
LEFT OUTER JOIN axiom_alto_group_skills GS
  ON GS.group_id = G.id AND US.skills_id = GS.skills_id
GROUP BY G.id, G.slug, G.name
ORDER BY skills_count DESC

In Django you can run that using a raw SQL query on the Group model: 在Django中,您可以在Group模型上使用原始SQL查询来运行它:

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

It should now be easy to see how to perform your second query. 现在应该很容易看到如何执行第二个查询。 That is, to count, for a specific user profile, the number of skills that user shares with each other user: 也就是说,对于特定的用户个人资料,计算用户彼此共享的技能数量:

SELECT U.id, U.slug, U.user, COUNT(US2.id) AS skills_count
FROM axiom_alto_userprofile U
INNER JOIN axiom_alto_userprofile_skills US1
  ON US1.userprofile_id = %s
LEFT OUTER JOIN axiom_alto_userprofile_skills US2
  ON US2.userprofile_id = U.id AND US2.skills_id = US1.id
GROUP BY U.id, U.slug, U.user
ORDER BY skills_count DESC

Again, in Django you can run that using a raw SQL query, this time on the UserProfile model: 再次,在Django中,您可以使用原始SQL查询运行它,这次是在UserProfile模型上运行:

UserProfile.objects.raw(''' ... SQL as above ... ''', [profile.id])
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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM