2013-04-05 47 views
1

我有一个模型USERPROFILE的是,与所谓的技巧 另一个表的多对多关系,我也有一个模型组与所谓的技巧多对多的Django的sql

另一个表的多对多的关系,我想作这个查询会计算出用户配置文件与每个组共有的技能数量。

我也希望进行查询,将算我的一个特定的用户配置文件已经在与他人的用户配置文件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') 

但我不知道如何将用户之间做

+1

请分享模式。 – karthikr 2013-04-05 19:49:34

+0

分享谢谢 – 2013-04-05 20:04:59

回答

1

您的第一个查询是计算特定用户配置文件中用户与每个组共享的技能数量。在你的文章中,你将展示一种使用子查询的方法。然而,在某些数据库子查询有更差的性能比加入,所以你可能会有兴趣看怎么办使用相同的结果联接,而不是子查询得到:

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 

在Django中,你可以运行使用raw SQL queryGroup型号:

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

现在应该很容易看到如何执行你的第二个查询。也就是说,来算,为特定用户配置文件中,与对方用户的用户共享的技能数量:

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 

同样,在Django可以运行使用原始的SQL查询,这个时候就UserProfile型号:

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

嗨Gareth谢谢,但事情是,skills_count不是唯一的子查询我有大量的子查询来获得其他计数,所以通过一个原始的做可能会更快,但要写一些SQL INNER JOIN ...我不知道我是否可以这样做,这将是一场噩梦......但你说得对,如果子查询花费太多时间,我可能会这样做。现在我在网站上没有那么多东西,但是将来我会这样做,所以我可能需要重新考虑并使用原始数据。 – 2013-04-06 18:34:14

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