在Tastypie水合物中获取`resource_uri`的内容

时间:2013-10-31 23:28:25

标签: django django-models tastypie

如何在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的记录?

1 个答案:

答案 0 :(得分:0)

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