【问题标题】:Tastypie access resource by parameter other than PKTastypie 通过 PK 以外的参数访问资源
【发布时间】:2016-07-27 22:49:34
【问题描述】:

我很难弄清楚如何提出这个问题。

我有一个模特User。目前,当我想访问特定用户时,我会转到 url:/api/v1/user/8/。虽然,所有用户都有唯一的用户名,所以我想使用 url 转到特定用户:/api/v1/user/joe/

也许是prepend_urls()

【问题讨论】:

    标签: python django api tastypie


    【解决方案1】:

    您需要在ModelResourceMeta 类(documentation)中使用detail_uri_name - 示例resources.py:

    from django.contrib.auth.models import User
    from tastypie.resources import ModelResource
    
    
    class UserResource(ModelResource):
        class Meta:
            queryset = User.objects.all()
            allowed_methods = ['get']
            detail_uri_name = 'username'
    

    【讨论】:

      猜你喜欢
      • 2021-10-23
      • 2013-11-30
      • 2012-07-07
      • 2018-03-26
      • 2013-10-17
      • 2019-06-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多