3

我想使用继承并从基础资源类继承我的所有资源。

您将在下面看到我到目前为止所做的尝试。我的问题是我现在需要添加元类,因为它现在似乎被覆盖了。如何才能做到这一点?

class BasedModelResource(ModelResource):
    class Meta:
        authentication = ApiKeyAuthentication()
        authorization = UserObjectsOnlyAuthorization()



class AccountResource(BasedModelResource):
    """
    Account Object Resource
    """
    class Meta:
        queryset = Account.objects.all()
        resource_name = 'account'
4

2 回答 2

5

有一个解决方法:

资源元继承

另请参阅:Tastypie 元继承

于 2013-06-11T12:55:35.440 回答
0

显式继承 Meta 并与 Resource 分开,因为它是一个单独定义的类:

class CommonMeta:
    abstract = True
    authentication = SessionAuthentication()
    authorization= Authorization()      
    allowed_methods = ('get', 'post', 'put', 'delete', 'patch')

class MyResource(ModelResource):
    ...

    class Meta(CommonMeta):
        # Inherits CommonMeta's properties. 
        # Note: abstract will become False automatically.

        queryset = MyModel.objects.all()
        resource_name = 'my_model'
于 2014-07-09T15:38:38.170 回答