1👍
✅
You should use reverse relation for such query. Start with adding related_name
to purchased
field of User
:
class User(AbstractUser):
...
purchased = models.ManyToManyField(Course, blank=True, related_name="buyers")
Then you can easily use that name in making queries based on relations:
Course.objects.filter(buyers=self.request.user).order_by('title')
Source:stackexchange.com