【发布时间】:2017-07-07 21:35:22
【问题描述】:
我知道我们可以从孩子那里引用父母的模型,但有没有其他方法可以让它使用?
抱歉这里有任何错别字。
假设父母是
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`
我的问题是,如果使用 Parent.objects.filter 调用查询,pa 是否可以与常规的 Parent 区别开来
我尝试使用isisntance,但对于pa,只有在Parent 为ch 时才适用,两者都适用。我想不出另一种方法来区分这一点。
另外,Parent 不会是抽象的。
附:我想过使用hasattr,但这也行不通。
提前致谢。
【问题讨论】:
标签: django inheritance field instance