2011-07-15 69 views
1
class Member(models.Model): 
    id = models.AutoField(unique=True, primary_key=True) 
    ... 

class Address(models.Model): 
    member = models.ForeignKey(Member) 

我有一個會員pk &可以檢索一個會員;但我想檢索指向該成員的可能不止一個地址(es)。我如何在django中實現這一點?如何檢索包含B外鍵的模型A的模型列表?

回答

1
Address.objects.filter(member=somemember) 

Address.objects.filter(member_id=somemember_pk) 
+0

或者member.address_set – GaretJax

0
class Address(models.Model): 
    member = models.ForeignKey(Member, related_name="addresses") 

然後使用member.addresses

+0

問題是:給定一個成員,檢索地址。 – canadadry

+0

解決方案是:'member.addresses' – DrTyrsa

相關問題