Django继承基类超级

时间:2013-06-11 11:36:54

标签: django tastypie

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

你会看到我到目前为止所尝试的内容。我的问题是我现在需要在元类中添加它似乎在此刻覆盖。如何才能做到这一点?

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



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

2 个答案:

答案 0 :(得分:5)

有一种解决方法:

Resource Meta Inheritance

另请参阅:Tastypie Meta Inheritance

答案 1 :(得分:0)

明确地并且与资源分开地继承Meta,因为它是一个单独定义的类:

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'