python from tastypie.resources import ModelResource from myapp.models import MyModel class MyModelResource(ModelResource): class Meta: queryset = MyModel.objects.all() resource_name = 'mymodel' python from tastypie.api import Api from myapp.api import MyModelResource v1_api = Api(api_name='v1') v1_api.register(MyModelResource()) urlpatterns = patterns('', url(r'^api/', include(v1_api.urls)), ) python from tastypie.authentication import Authentication from tastypie.authorization import Authorization class MyAuthentication(Authentication): def is_authenticated(self, request, **kwargs): token = request.GET.get('token') if token and MyUser.objects.filter(token=token).exists(): return True return False class MyAuthorization(Authorization): def is_authorized(self, request, object=None): class MyModelResource(ModelResource): class Meta: authentication = MyAuthentication() authorization = MyAuthorization() python from tastypie.filters import BaseFilterSet, MultipleChoiceFilter from tastypie.constants import ALL, ALL_WITH_RELATIONS class MyModelFilterSet(BaseFilterSet): category = MultipleChoiceFilter(name='category', choices=CATEGORY_CHOICES) class Meta: model = MyModel fields = ['category', 'price'] order_by = ['price'] class MyModelResource(ModelResource): class Meta: queryset = MyModel.objects.all() resource_name = 'mymodel' filtering = { 'category': ALL, 'price': ALL, } ordering = ['price'] filter_class = MyModelFilterSet


上一篇:
下一篇:
切换中文