Note that there are some explanatory texts on larger screens.

plurals
  1. POmany to many django sql
    text
    copied!<p>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</p> <p>I would like to make a query that will count me the number of skills that the userprofile have in common with each group.</p> <p>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</p> <p>Could someone help me to do this. Thank you</p> <p>the models :</p> <pre><code>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) </code></pre> <p>So I found a way to do it for groups</p> <pre><code>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') </code></pre> <p>But I don't know how to do it between users</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload