Tastypie:为所有资源定义身份验证

Tastypie: define authentication for all resources

为所有 tastypie 资源定义 authentication/authorization 的最佳方法是什么?我有很多资源,不想每次都复制身份验证行。现在,我定义了一个函数

def create_auth():
    return MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication())

并在每个资源的元数据中调用它 class:

class SomeResource(ModelResource):
    class Meta:
        authentication = create_auth()

有更好的解决办法吗?是 'metametaclass' 解决方案 possible/better?例如。

class AuthMeta:
    authentication = MultiAuthentication(ApiKeyAuthentication(), BasicAuthentication())

class SomeResource(ModelResource):
    class Meta(AuthMeta):
        # further settings

class SomeOtherResourceNonORM(Resource):
    class Meta(AuthMeta):
        # further settings

您应该按照 tastypie and django 的建议执行 metametaclass

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

更新: 使用正确的基础资源