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

标签 python django model tastypie

我正在尝试覆盖 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' ) , 我正在设置变量 foo关于资源MyResource , 到 modelA 上的变量称为 variableOnModelA .这很好用。

但我正在尝试制作 variableOnModelAMyResource 的标识符, 这样我就可以做 /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

关于python - Tastypie 使用自定义 detail_uri_name,类型不匹配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38669717/

相关文章:

python - Django 支付集成

django - 从 Arduino 到 Django 数据库的 HTTP POST 数据

php - 在 cakePHP 中查找具有 Containable 行为的条件

sharepoint - 使用 Sharepoint 2010 Client OM 指定要加载的项目字段

python - 如何使用 SQLAlchemy 在一次提交中切换唯一行的两个字段?

python - SQLAlchemy 在提交前使用自动增量获取主键

python - 如果参数在 Python 3 中为 False,则寻找惯用的方法来评估 False

Python 最佳实践 : initializing variables

python - 在 Django Rest 框架分页中添加新值

ruby-on-rails - 如何基于自定义模型方法查询Rails/ActiveRecord模型?