【发布时间】:2009-10-02 18:56:34
【问题描述】:
我想在我的经理方法baz 中访问Foo 实例foo:
foo.bar_set.baz()
baz 通常会采用Foo 类型的参数:
BarManager(models.Manager):
def baz(self, foo=None):
if foo is None:
# assume this call originates from
# a RelatedManager and set `foo`.
# Otherwise raise an exception
# do something cool with foo
这样,上面的第一个查询和下面的查询是一样的:
Bar.objects.baz(foo)
Bar 会有一个指向 Foo 的 ForeignKey:
class Bar(models.Model):
foo = models.ForeignKey(Foo)
objects = BarManager()
【问题讨论】:
标签: python django django-models django-orm