为所有tastypie资源定义身份验证/授权的最佳方法是什么?我有很多资源,不想每次都复制身份验证行。现在,我定义了一个函数
def create_auth():
return MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication())
并在每个资源的元类中调用它:
class SomeResource(ModelResource):
class Meta:
authentication = create_auth()
有更好的解决方案吗? “metametaclass”解决方案是否可行/更好?例如。
class AuthMeta:
authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication())
class SomeResource(ModelResource):
class Meta(AuthMeta):
# further settings
class SomeOtherResourceNonORM(Resource):
class Meta(AuthMeta):
# further settings
答案 0 :(得分:0)
您应该metametaclass
推荐tastypie和django。
classs YourBaseResource(ModelResource):
class Meta:
authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication())
class SomeResource(YourBaseResource):
class Meta(YourBaseResource.Meta):
# further settings
class SomeOtherResourceNonORM(YourBaseResource):
class Meta(YourBaseResource.Meta):
# further settings
使用正确的基本资源更新