【问题标题】:django-tastypie REST update or delete resource with usernamedjango-tastypie REST 使用用户名更新或删除资源
【发布时间】:2013-05-11 06:55:44
【问题描述】:

您好,我正在使用 django sweetpie,如果我提供这样的 id,我可以更新(PUT)资源:

http://mysite.com:8000/api/v1/user/1/

但我的问题是,我可以在没有 id (pk) 而是使用用户名的情况下进行更新吗?如果是,我该怎么做? 它会是这样的:

http://mysite.com:8000/api/v1/user/username/

【问题讨论】:

    标签: python django rest python-2.7 tastypie


    【解决方案1】:

    这在tastypie cookbook中有描述

    class UserResource(ModelResource):
       class Meta:
          queryset = User.objects.all()
    
       def prepend_urls(self):
          return [
            url(r"^(?P<resource_name>%s)/(?P<username>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
        ]
    

    【讨论】:

      猜你喜欢
      • 2012-08-17
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多