0

As i have many user. i want to fetch the name of users join a particular session in Players_Participating

class MyUser(AbstractBaseUser):

    user_name=models.CharField(max_length=10,blank=True,null=True,unique=True)

    def __str__(self):
        return str(self.user_name)

class Session(models.Model):

    Host=models.ForeignKey(MyUser,on_delete=models.CASCADE,related_name='host')
    game=( 
        ('cricket','cricket'),
        ('football','football'),
        ('basketball','basketball'),
        ('hockey','hockey'),
        ('gym','gym'),
        ('baseball','baseball'),
    )

    Sport=models.CharField(max_length=20,choices=game)

    Players_Participating=models.ManyToManyField(MyUser,related_name='related')
    def __str__(self):

        return str(self.Host)
ans2human
  • 2,300
  • 1
  • 14
  • 29

1 Answers1

0

You can try using prefetch_related like this:

s = Session.objects.get(that particular session).prefetch_related('host')

Also, this question has been answered previously here AND here You'll find a lot of links there that will be helpful to you. :)

VeryBhatti
  • 119
  • 7