Django查询ForeignKey字段的值

时间:2016-03-25 15:47:47

标签: django

我想要获取main_contact.paid=True所有的Runner记录。我不能训练Django的电话。

以下是模型:

class Main_contact(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    email = models.EmailField()
    tel = models.CharField(max_length=20)
    token = models.CharField(max_length=255)
    paid = models.BooleanField(default=False)

class Runner(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)
    age = models.IntegerField(default=0)
    fee = models.IntegerField(default=0)
    main_contact = models.ForeignKey(Main_contact)

1 个答案:

答案 0 :(得分:1)

尝试遍历以下关系:

Runner.objects.filter(main_contact__paid=True)