2013-04-05 2 views
1

가 나는 또한 그 기술많은 많은 장고 내가 할 싶습니다

라는 다른 테이블과 manytomany 관계로 모델 그룹이 기술 라는 다른 테이블과 manytomany 관계로 그 사용자 프로필 모델이 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

+0

감사합니다. –

답변

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 

장고 당신은을 사용하는 것을 실행할 수 있습니다 Group 모델 raw SQL query :

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 

다시 장고에 당신이 실행 UserProfile에 원시 SQL 쿼리를 사용하여,이 시간이 있습니다 : 즉, 특정 사용자 프로필에 대해 각각 다른 사용자와 사용자 주가는 기술의 수를 계산하는 모델 :

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

안녕하세요 Gareth 감사하지만 문제는 skill_count가 원시를 통해 수행하는 것이 더 빠를 수 있지만 다른 많은 수의 하위 쿼리가있는 유일한 하위 쿼리가 아니라 많은 내부 내부 조인과 함께 SQL을 작성하는 것입니다. 나는 그것이 악몽이 될 것이라는 것을 내가 할 수 있는지 모르겠다.하지만 하위 쿼리가 너무 많은 시간이 걸리면 끝낼 수도있다. 지금 당장 사이트에 그다지 많은 것들이 없지만 앞으로는 원산지를 다시 생각하고 사용해야 할 수도 있습니다. –

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') 
관련 문제