I have the following few Classes in my models.py
class Account(models.Model):
user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
name = models.CharField(max_length=255, null=True)
class Challenge(models.Model):
match_time = models.DateTimeField(null=True)
totalPlayers = models.IntegerField(default=1)
status = models.CharField(max_length=100, default="pending")
created_by = models.ForeignKey(Account, on_delete=models.CASCADE)
expiry_time = models.DateTimeField(null=True)
class Challenge_Participant(models.Model):
player = models.ForeignKey(Account, on_delete=models.CASCADE)
challenge = models.ForeignKey(Challenge, on_delete=models.CASCADE)
status = models.CharField(max_length=100, default="pending")
What I am trying to do is actually access all the Challenge objects in which the logged in user is participating (so in Challenge_Participant, player will be equal to request.user.account and challenge will be equal to the challenge being searched) and exclude those challenges which are created by the User itself (because the challenges in which user is participating himself, there is an object of Challenge_Participant for this case as well)
I am unable to make a query to extract all the Challenges in which request.user is part of.
I am trying to do something like this,
challenges = Challenge.objects.filter().exclude(created_by = request.user.account)
I want to Filter Out those challenges in which the Challenge_Participant objects have user equal to the logged in user and challenge equal to the challenge being searched. I can't seem to figure out what I can write in the Filter comamnd in such a case
From those results of challenges, I am excluding all the challenges which are created by the user himself.