Tastypie 使用自定义 detail_uri_name,类型错误不匹配

Tastypie using custom detail_uri_name, mismatched type error

我正在尝试覆盖 get_bundle_detail_data

class MyResourse(ModelResource):
     foo = fields.CharField( attribute = 'modelA__variableOnModelA' )
     def get_bundle_detail_data(self, bundle):
         return bundle.obj.foo
     class Meta:
         resource_name='resource'

在代码行 foo = fields.CharField( attribute = 'modelA__variableOnModelA' ) 中,我将资源 MyResource 上的变量 foo 设置为 modelA 上名为 variableOnModelA 的变量.这很好用。

但我试图让 variableOnModelA 成为 MyResource 的标识符,这样我就可以 /api/v1/resource/bar/ 获取详细的 MyResource 变量 foo 设置为 bar

我遇到的问题是错误:Invalid resource lookup data provided (mismatched type).这个错误在说什么?

终极问题:如何使用foo作为detail_uri_name

编辑 型号:

class AgoraUser(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True, related_name='agora_user')
    class Meta:
        db_table = 'agora_users'

网址:

full_api = Api(api_name='full')
full_api.register(AgoraUserResourse())
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^', include(full_api.urls)),
    url(r'^', include(min_api.urls)),
    url(r'^search/', include('haystack.urls')),
    url(r'^accounts/login/$', auth_views.login, {'template_name': 'login.html'}, name='login'),
]

实际资源:

class AgoraUserResourse_min(ModelResource):
    username = fields.CharField(attribute = 'user__username' )
    class Meta:
        resource_name='user'
        #detail_uri_name = 'user__username'
        queryset = AgoraUser.objects.all()
        allowed_methods = ['get', 'put', 'post']
        authentication = AgoraAuthentication()
        authorization = AgoraAuthorization()
    def get_bundle_detail_data(self, bundle):
        return bundle.obj.username

您似乎需要为您的资源覆盖 detail_uri_kwargs

我得到了这样的结果:

from tastypie import fields
from tastypie.resources import ModelResource
from tastypie.bundle import Bundle

from .models import AgoraUser


class AgoraUserResourse(ModelResource):
    username = fields.CharField(attribute='user__username')
    class Meta:
        resource_name='user'
        detail_uri_name = 'user__username'
        queryset = AgoraUser.objects.all()
        allowed_methods = ['get', 'put', 'post']
        # authentication = AgoraAuthentication()
        # authorization = AgoraAuthorization()

    def detail_uri_kwargs(self, bundle_or_obj):
        if isinstance(bundle_or_obj, Bundle):
            bundle_or_obj = bundle_or_obj.obj

        return {
            'user__username': bundle_or_obj.user.username
        }

    def get_bundle_detail_data(self, bundle):
        return bundle.obj.username