我为Django-Tastypie提供了类似的代码:
class SpecializedResource(ModelResource):
class Meta:
authentication = MyCustomAuthentication()
class TestResource(SpecializedResource):
class Meta:
# the following style works:
authentication = SpecializedResource.authentication
# but the following style does not:
super(TestResource, meta).authentication
我想知道在没有硬编码超类名称的情况下访问超类的元属性的正确方法是什么。
答案 0 :(得分:8)
在您的示例中,您似乎正在尝试覆盖超类元的属性。为什么不使用元继承?
class MyCustomAuthentication(Authentication):
pass
class SpecializedResource(ModelResource):
class Meta:
authentication = MyCustomAuthentication()
class TestResource(SpecializedResource):
class Meta(SpecializedResource.Meta):
# just inheriting from parent meta
pass
print Meta.authentication
输出:
<__main__.MyCustomAuthentication object at 0x6160d10>
以便TestResource
的{{1}}继承自父元(此处为身份验证属性)。
最后回答问题:
如果你真的想要访问它(例如将东西附加到父列表等),你可以使用你的例子:
meta
或没有硬编码 超级类:
class TestResource(SpecializedResource):
class Meta(SpecializedResource.Meta):
authentication = SpecializedResource.Meta.authentication # works (but hardcoding)