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

时间:2016-05-26 12:14:40

标签: python django tastypie

为所有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

1 个答案:

答案 0 :(得分:0)

您应该metametaclass推荐tastypiedjango

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
使用正确的基本资源

更新