开发者

How to retrieve the list of models for model A which contains a foreign key to B?

class Member( models.Model ):
    id = models.AutoField(unique=True, primary_key=True)
    ...

class Address( models.Model ):
  开发者_Python百科  member = models.ForeignKey( Member )

I have the pk for a Member & can retrieve a Member; but I want to retrieve the possibly more than one Address(es) that point to that Member. How do I achieve that in django ?


Address.objects.filter(member=somemember)

or

Address.objects.filter(member_id=somemember_pk)


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

Then use member.addresses.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜