开发者

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

开发者 https://www.devze.com 2023-03-20 06:38 出处:网络
class Member( models.Model ): id = models.AutoField(unique=True, primary_key=True) ... class Address( models.Model ):
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

精彩评论

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