I have two models, a user model and a sport model. These two models have a m2m relationship as a user can do multiple sports and a sport can be played by more than one user. I want to get a query set of a user's sports, so only the sports a specific user does shows up for that user.
I have created a context_processors.py file so that the information is available on all pages of the site. The problem is that I don't know how to query the database to get this information.
These are my models:
class Sport(models.Model):
name = models.CharField(max_length=100, null=False)
class User(AbstractUser):
...
sports = models.ManyToManyField(Sport)
Right now, I get all the sports available, like this
def test(request):
sports = Sport.objects.all()
return {
'x': sports
}
I just want to get the sports the requested user is assigned to in the sports variable.