Django - 在查询中加入2个模型

时间:2011-03-09 20:39:45

标签: python django django-models django-queryset

class Person(models.Model):
    name = models.CharField(max_length=20)
    employer = models.CharField(max_length=20)

class Phone(models.Model):
    person = models.ForeignKey(Person)
    number = models.CharField(max_length=10)
    vendor = models.CharField(max_length=20)

如何编写查询以检索Phone.number = 945678987Person.employer = 'xyz'所有的电话对象?

2 个答案:

答案 0 :(得分:9)

"Lookups that span relationships"

Phone.objects.filter(number=u'945678987', person__employer=u'xyz')

答案 1 :(得分:4)

Phone.objects.filter(number='945678987', person__employer='xyz')