特定の属性を持つ子どもたちとのインスタンスのクエリセットジャンゴMPTTは、このようなモデルfoobarにして、私は<a href="https://pypi.python.org/pypi/django-mptt/" rel="nofollow noreferrer">Django MPTT</a>を使用してい
私は特定の属性を持つ子どもを持つすべてのFoobarsを選択したい場合[x for x in Foobar.objects.all() if x.get_children().exists()]
(のようなpublished
)が、私はこれを行うことができます。
[x for x in Foobar.objects.all() if x.get_children().filter(published=True).exists()]
しかし、これを1つのクエリで行う方法はありません。まあ、親であることの純粋なプロパティは__isnull
フィルターを経由してのためにフィルタリングされ
class Example(models.Model):
related_foobar = models.ForeignKey(
Foobar,
limit_choices_to=Q(....), # How do I make this work?
)
'Foobar'sメタの' parent_attr'で 'parent'フィールドが指定されていますか?もしそうなら、 'related_name'は何ですか? – schwobaseggl
@schwobaseggl私はその情報を含めるために私の答えを編集しました。 – Flimm