django many to many relationship can't query properly

时间:2015-09-14 16:16:45

标签: django

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~~

1 个答案:

答案 0 :(得分:4)

您已告诉Django改为使用属性Com_Team(使用related_name='Com_Team'):

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