我是Django&Tastypie的新手.我想只返回查询中的一个对象.我几乎尝试了一切,似乎无法找到解决方案.这是我的代码如下:
class ProfileResource(ModelResource):
person = fields.ForeignKey(UserResource, 'user', full=True)
class Meta:
queryset = Person.objects.all()
resource_name = 'profile'
authentication = BasicAuthentication()
authorization = DjangoAuthorization()
serializer = Serializer(formats=['json'])
Run Code Online (Sandbox Code Playgroud)
现在我遇到问题的部分是如何使用单个资源返回单个用户对象request.user.
小智 4
如果您只想显示一种资源,我可能会创建新的资源视图(名为 my_profile),该视图将使用 kwargs 中的用户调用正常的详细信息视图,并删除其他 url:
from django.conf.urls import url
from tastypie.utils import trailing_slash
class ProfileResource(ModelResource):
...
def base_urls(self):
return [
url(r"^(?P<resource_name>%s)%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_my_profile'), name="api_dispatch_my_profile")
]
def dispatch_my_profile(self, request, **kwargs):
kwargs['user'] = request.user
return super(ProfileResource, self).dispatch_detail(request, **kwargs)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
653 次 |
| 最近记录: |