[英]How to propagate parent attributes in Django models?
Is there a way to propagate child attributes in parent class without having to access it by its related name? 有没有一种方法可以在父类中传播子属性,而不必通过其相关名称访问它? Please consider the following example:
请考虑以下示例:
class BaseA(models.Model):
pass
class BaseB(models.Model):
a = models.ForeignKey('BaseA')
class A(BaseA):
base = models.OneToOneField('BaseA', related_name='related')
field = models.IntegerField(default=0)
def some_method(self):
pass
class B(BaseB):
base = models.OneToOneField('BaseB')
# code in view
model_b = B()
model_b.a.some_method()
I cannot access the some_method method because model_b.a returns a BaseA() instance instead of A() , if I want to access this method I have to: 我无法访问some_method方法,因为model_b.a返回一个BaseA()实例而不是A() ,如果我想访问此方法,我必须:
model_b.a.related.some_method()
# or
model_b.a.related.field
Is there a way to propagate the properties and methods from the base class so I won't have to access using the related name? 有没有办法从基类传播属性和方法,这样我就不必使用相关名称进行访问了吗?
Why not just add this to BaseA
as a util: 为什么不将它作为util添加到
BaseA
:
class BaseA(models.Model):
def some_method(self):
return self.related.some_method()
If this is called for an non- A
BaseA
, it will raise an AttributeError
which is plausible enough. 如果这就是所谓的一个非
A
BaseA
,它会引发一个AttributeError
是顺理成章。 If you want propagate all methods, you can override __getattr__
. 如果要传播所有方法,则可以覆盖
__getattr__
。 This will only look at self.related
if the attribute isn't found on the BaseA
instance itself: 如果在
BaseA
实例本身上找不到该属性,则只会查看self.related
:
class BaseA(models.Model):
def __getattr__(self, item):
return getattr(self.related, item)
But adding attributes dynamically like this should be a last resort, one that makes code navigation and debugging harder, and renders other IDE features like code completion unusable. 但是,像这样动态地添加属性应该是最后的选择,这会使代码导航和调试更加困难,并使其他IDE功能(如代码完成)无法使用。 IMO, it is preferable to put all the methods you might actually want to call on
BaseA
instances in the class explicitly. IMO,最好将您可能实际要调用的所有方法明确地放在类的
BaseA
实例上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.