您可以将骨干集合/模型指向任何您想要的url并解析您想要的骨干“子类”中的响应。
Model.url:
http://documentcloud.github.com/backbone/docs/backbone.html#section-43
Collection.parse:
http://documentcloud.github.com/backbone/docs/backbone.html#section-69
您可以设置一次性请求处理,可以返回一些JSON为骨干,以解析/摄取不活塞或tastypie。但是,是的,这对于使用django的综合REST来说是两个很好的解决方案。
这里有一些很好的提示:http://joshbohde.com/blog/backbonejs-and-django使用骨干与tastypie。
使用tastypie,您可以通过自定义授权/验证来限制对api的访问。
http://django-tastypie.readthedocs.org/en/latest/authentication_authorization.html
您可以创建一个授权方案,该方案可以确保对象列表进行过滤,只在该用户“拥有”这样的对象,东西:
class PerUserAuthorization(Authorization):
def apply_limits(self, request, object_list):
if request and hasattr(request, 'user'):
if request.user.is_authenticated():
object_list = object_list.filter(user=request.user)
return object_list
return object_list.none()
或者/附加,您可以通过覆盖ModelResource.apply_authorization_limits
方法来创建只返回用户对象的资源,并通过覆盖obj_create
方法自动将用户与创建的对象关联起来,如下所示:
class PerUserModelResource(ModelResource):
def obj_create(self, bundle, request=None, **kwargs):
return ModelResource.obj_create(self, bundle, request, user=request.user)
def apply_authorization_limits(self, request, object_list):
return object_list.filter(user=request.user)
然后,您可以从PerUserModelResource继承和/或使PerUserAuthorization成为资源的授权。
class ImageGroupResource(PerUserModelResource):
study = fields.ForeignKey(StudyResource, "study")
uploads = fields.ToManyField('cm.api.UploadResource', 'uploads', null=True)
class Meta:
queryset = ImageGroup.objects.all()
list_allowed_methods = ['get', 'post']
detail_allowed_methods = ['get', 'post', 'put', 'delete']
resource_name = 'cm/imagegroup'
authorization = PerUserAuthorization()
filtering = {
'name': ALL,
'created_dt': ['exact', 'range', 'gt', 'gte', 'lt', 'lte'],
}
骨干和django-tastypie有很好的记录。花时间建立一个简单的概念验证并阅读文档几次。他们像豌豆和胡萝卜一样走到一起。
感谢您的回复。关于权限和组,是否可以实现和使用它们? – imns
当然,只需使用相同的方法,而不是仅使用用户的身份来应用限制,而是使用组状态或任意条件。 –