0

resource_uri在 Tastypie 通话期间如何解决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

4

1 回答 1

0

Resource.get_via_uri(url)( doc ) 可能会派上用场。

于 2013-11-06T11:33:51.647 回答