简体   繁体   中英

Which function will be called when using tastypie resource

I want to understand this point.

this is my url: repositories/api/2/repositories/?format=json

this is urls.py :

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

this is 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

It works fine, but I want to know why why obj_get_list is called even if I don't specify it, and what about if I want to use other functions like obj_get ? Which function will be called by default?

URL : repositories/api/2/repositories/?format=json

will list all available objects .

Now using obj_get_list U can override the way json response will display. you can customize, limit the response (here in your case posts json dict )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM