I am currently writing an application that matches users based on similar interests, the subjects they study, where they live, etc.
I have my user creation/login system written and working. However, I am struggling to get started with the matching algorithm. After logging in to the site, I'd like it to be able iterate through other users' interests (which is a many-to-many field to the interest model) and then return a list of similar users, users that have one or more interests the same as, or are on the same course as the logged in user.
What would be the best way to implement this into a Django app?
Any help and advice is greatly appreciated!
I have attached a snippet of my models code for reference. Many thanks in advance
models.py
class Interest(models.Model):
title = models.TextField()
def __unicode__(self):
return self.title
class Location(models.Model):
location = models.CharField(max_length=50)
def __unicode__(self):
return self.location
class Course(models.Model):
title = models.CharField(max_length=40)
faculty = models.CharField(max_length=40)
def __unicode__(self):
return self.title
class MyUser(AbstractBaseUser):
email = models.EmailField(
verbose_name='email address',
max_length=255,
unique=True,
)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=40)
date_of_birth = models.DateField()
course = models.ForeignKey(Course, null=True)
location = models.ForeignKey(Location, null=True)
interests = models.ManyToManyField(Interest, null=True)
bio = models.TextField(blank=True)
objects = MyUserManager()
class MyUserManager(BaseUserManager):
def create_user(self, email, date_of_birth, password=None):
"""
Creates and saves a User with the given email, date of
birth and password.
"""
if not email:
raise ValueError('Users must have an email address')
user = self.model(
email=MyUserManager.normalize_email(email),
date_of_birth=date_of_birth,
)
user.set_password(password)
user.save(using=self._db)
return user
def create_superuser(self, email, date_of_birth, password):
"""
Creates and saves a superuser with the given email, date of
birth and password.
"""
u = self.create_user(email=email,
password=password,
date_of_birth=date_of_birth
)
u.is_admin = True
u.save(using=self._db)
return u