私は子供から親のモデルを参照できることを知っていますが、それを他の方法で持つ方法はありますか?親クラスが子クラスにこのようなフィールドがあるかどうかを確認できますか?
ご迷惑をおかけして申し訳ありません。
のは、親が私の質問は何
class Parent(Model):
has_this = models.Charfield(max_length=128)
class Child(Parent):
has_that = models.Boolean(default=True)
ch = Child.objects.filter(id=1).first() // this will be instance of both Parent and Child as expected
pa = Parent.objects.filter(id=1).first() // is actually return the same as above but does not has the `Child` field `has_that`
がある場合としましょう、クエリが、私はisisntance
が、用を使用してみましたParent.objects.filter
を使用して呼び出された場合、正規Parent
から差動にpa
ための方法がありますpa
の場合は、Parent
の場合はch
となります。私はこれを別の方法で考えることはできません。
また、Parent
は抽象的ではありません。
P.S.私はhasattr
を使用することを考えましたが、これもうまくいかないでしょう。
ありがとうございます。
[Djangoモデルの継承 - クエリ内の親クラスのインスタンスのみが必要です。](https://stackoverflow.com/questions/11853850/django-model-inheritance-only-want-instances-of-parent -class-in-a-query) – dhke