【问题标题】:Getting Contents of `resource_uri` During Tastypie Hydrate在 Tastypie Hydrate 期间获取 `resource_uri` 的内容
【发布时间】:2013-10-31 23:28:25
【问题描述】:

如何在 Tastypie hydrate 通话期间解决 resource_uri 问题?

我将以下数据对象传递给 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

这是可行的,但在resource_uri 上调用split 似乎不是最优雅的方法。

在给定resource_uri 的情况下,有没有更有效的方法来提取记录?

【问题讨论】:

    标签: django django-models tastypie


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-18
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      • 2017-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多