Tastypie: How can I create an "abstract" ModelResource class with a Meta class

121 views Asked by At

Using django-tastypie I'd like to create a child class of ModelResource to define some common things, which all my other API resource classes would inherit from. e.g.:

class MyModelResource(ModelResource):
    class Meta:
        authentication = SessionAuthentication()
        authorization = APIAuthorization()
        always_return_data = True

    # Some other common methods here


class UserResource(MyModelResource):
    class Meta(MyModelResource.Meta):
        queryset = User.objects.all()

However, if I do this, I get the following error:

django.core.exceptions.ImproperlyConfigured: ModelResource (MyModelResource) requires Meta.object_class or Meta.queryset

So I must set object_class or queryset in MyModelResource.Meta, even though it's a kind of 'Abstract' class.

I can set abstract = True in MyModelResource.Meta, and this works, so long as I remember to set abstract = False in every one of its child classes.

Is there a better way to do this?

0

There are 0 answers