Yi Tian
Yi Tian

Reputation: 81

django many to many relationship can't query properly

I have two models using many2many relationship , but I can not make the correct query using one model to another. My code :

class Competition(models.Model):
    name = models.CharField(max_length=30)
    teams = models.ManyToManyField(Team, related_name='Com_Team')
class Team(models.Model):
    name = models.CharField(max_length=30, unique=True)

But when I want to make a query like this:

com = Team.objects.get(id = 1).competition_set.all()

I'v been told that:

'Team' object has no attribute 'competition_set'

I just don't know what to do next... Thanks very much~~

Upvotes: 1

Views: 29

Answers (1)

Ben
Ben

Reputation: 6767

You've told Django to call the attribute Com_Team instead (with related_name='Com_Team'):

Team.objects.get(id=1).Com_Team.all()

Upvotes: 4

Related Questions