使用tastypie资源时将调用哪个函数

时间:2014-03-06 11:37:23

标签: python django tastypie

我想明白这一点。

这是我的网址:repositories/api/2/repositories/?format=json

这是urls.py

    repositories_resource = RepositoriesResource()
    (r'^api/2/', include(repositories_resource.urls)),

这是api.py

class RepositoriesResource(Resource):
  fs_repositories = fields.CharField(attribute='fs_repositories')
  full_path = fields.CharField(attribute='full_path')
  path = fields.CharField(attribute='path')
  permissions = fields.CharField(attribute='permissions')
  perms_read = fields.ListField(attribute='perms_read')
  perms_write = fields.ListField(attribute='perms_write')
  perms_no_access = fields.CharField(attribute='perms_no_access')   

  class Meta:
    resource_name = 'repositories'
    authorization= Authorization()
  def obj_get_list(self, request=None, **kwargs):
    request.GET = request.GET.copy()
    request.GET.update({"output":"json"})

    frs=svn_repositories(request)

    posts = []
    for l in frs.fs_repositories:
        posts.append(dict2obj({'fs_repositories': l,'full_path':l.full_path,'path':l.path,'permissions':l.permissions,'perms_read':l.perms_read,'perms_write':l.perms_write,'perms_no_access':l.perms_no_access }))      
    return posts

它工作正常,但我想知道为什么即使我没有指定obj_get_list,为什么要调用obj_get,如果我想使用{{1}}等其他函数呢?默认情况下会调用哪个函数?

1 个答案:

答案 0 :(得分:0)

网址:repositories/api/2/repositories/?format=json

将列出所有可用对象。

现在使用obj_get_list U可以覆盖json响应显示的方式。你可以自定义,限制响应(在你的情况下posts json dict)