如何在Tastypie resource_uri
来电期间解析hydrate
?
我将以下数据对象传递给Tastypie资源:
{
'date': u'2013-10-31 15:06',
'visitor': u'/visitorlog/api/v1/person/33/',
'purpose': u'Testing'
}
我想取visitor
并将整个记录拉入hydrate
函数中,该函数会在字段中填入一些额外信息。
我目前通过拆分id
并执行搜索来执行此操作:
def hydrate_meta(self, bundle):
'''
This will populate the `meta` field with a snapshot of the employee record, if the `empid` is set. This is done so if the employee record changes we retain certain information at the time of the visit.
'''
split_id = bundle.data['visitor'].split('/')
# the id of `visitor` is in `split_id[-2]
# get the record of this visitor from the Django model
person_record = Person.objects.get(pk = split_id[-2])
# ... snipped ... create the `meta_value` object
bundle.data['meta'] = meta_values
return bundle
这是有效的,但在split
上调用resource_uri
似乎不是最优雅的方法。
是否有更有效的方法来提取resource_uri
的记录?