露出"虚拟"在tastypie视野? [英] Exposing "virtual" field in a tastypie view?
问题描述
我想用tastypie公开相同类型的某些对象来创建一个视图,但具有下列<打击>两个击>三捻:
I want to create a view using tastypie to expose certain objects of the same type, but with the following two three twists:
- 我需要使用三个单独的查询对象;
- 我需要添加一种对底层模型中存在的字段,并且该字段的值取决于它来自查询;和
- 在该数据将每个用户的(所以我需要挂钩到的是得到一个请求的方法之一)。
我不清楚如何挂钩到tastypie生命周期,以实现这一目标。推荐的方法添加一个虚拟字段中的脱水方法,只知道捆绑它的经营上。
I'm not clear on how to hook into the tastypie lifecycle to accomplish this. The recommended way for adding a "virtual" field is in the dehydrate method, which only knows about the bundle it's operating on.
更糟糕的是,有加入查询集没有正式的办法。
Even worse, there's no official way to join querysets.
我的问题会消失,如果我能得到tastypie接受的东西比一个QuerySet等。在这种情况下,我可以通过它我对象的子类列表,与其他字段添加。
My problem would go away if I could get tastypie to accept something other than a queryset. In that case I could pass it a list of subclasses of my object, with the additional field added.
我接受任何其他合理的解决方案。
I'm open to any other sensible solution.
编辑:增加扭转3 - 每个用户的数据。
Added twist 3 - per-user data.
推荐答案
好了,这是我的解决方案。 code是如下。
OK, so this is my solution. Code is below.
注意要点:
- 的工作基本都在
obj_get_list
完成。这就是我跑我的查询,其访问请求。 - 我可以返回从
obj_get_list
列表。 - 我可能会覆盖所有其他
OBJ _相当于其他操作(如
方法,obj_get
*obj_create
等),如果我想他们是可用的。 - 因为我没有一个
查询集
在元
,我需要提供一个object_class
来告诉tastypie的自省哪些领域提供。 - 要揭露我的虚拟属性(我在创建
obj_get_list
),我需要添加一个字段声明吧。 - 在我注释掉的过滤器和授权限制,因为我不需要他们现在。我需要实现他们自己,如果我需要他们。
- The work is basically all done in
obj_get_list
. That's where I run my queries, having access to the request. - I can return a list from
obj_get_list
. - I would probably have to override all of the other
obj_*
methods corresponding to the other operations (likeobj_get
,obj_create
, etc) if I wanted them to be available. - Because I don't have a
queryset
inMeta
, I need to provide anobject_class
to tell tastypie's introspection what fields to offer. - To expose my "virtual" attribute (which I create in
obj_get_list
), I need to add a field declaration for it. - I've commented out the filters and authorisation limits because I don't need them right now. I'd need to implement them myself if I needed them.
code:
from tastypie.resources import ModelResource
from tastypie import fields
from models import *
import logging
logger = logging.getLogger(__name__)
class CompanyResource(ModelResource):
role = fields.CharField(attribute='role')
class Meta:
allowed_methods = ['get']
resource_name = 'companies'
object_class = CompanyUK
# should probably have some sort of authentication here quite soon
#filters does nothing. If it matters, hook them up
def obj_get_list(self, request=None, **kwargs):
# filters = {}
# if hasattr(request, 'GET'):
# # Grab a mutable copy.
# filters = request.GET.copy()
# # Update with the provided kwargs.
# filters.update(kwargs)
# applicable_filters = self.build_filters(filters=filters)
try:
#base_object_list = self.get_object_list(request).filter(**applicable_filters)
def add_role(role):
def add_role_company(link):
company = link.company
company.role = role
return company
return add_role_company
director_of = map(add_role('director'), DirectorsIndividual.objects.filter(individual__user=request.user))
member_of = map(add_role('member'), MembersIndividual.objects.filter(individual__user=request.user))
manager_of = map(add_role('manager'), CompanyManager.objects.filter(user=request.user))
base_object_list = director_of + member_of + manager_of
return base_object_list #self.apply_authorization_limits(request, base_object_list)
except ValueError, e:
raise BadRequest("Invalid resource lookup data provided (mismatched type).")
这篇关于露出&QUOT;虚拟&QUOT;在tastypie视野?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!