Django 在水合物生成期间,使用不同的资源内容填充TastyPie字段?
在Django 在水合物生成期间,使用不同的资源内容填充TastyPie字段?,django,django-models,tastypie,Django,Django Models,Tastypie,在过程中,如何最好地用另一条记录的内容填充tastype资源中的字段 我在资源中定义了以下函数,用于填充模型中的“meta”字段: def hydrate_meta(self, bundle): ''' This will popular the `meta` field with a snapshot of the employee record, if the `empid` is set. ''' if bundle.data['visitor']['em
过程中,如何最好地用另一条记录的内容填充tastype资源中的字段
我在资源中定义了以下函数,用于填充模型中的“meta”字段:
def hydrate_meta(self, bundle):
'''
This will popular the `meta` field with a snapshot of the employee record, if the `empid` is set.
'''
if bundle.data['visitor']['empid']:
# split the employee resource so we can get to the actual empid number
empid_split = bundle.data['visitor']['empid'].split('/')
# query the employee record, the `-2` index is the actual empidinteger
meta_values = Employee.objects.filter(empid= empid_split[-2]).values('division', 'program', 'subprogram', 'base_city', 'base_state', 'base_country', 'group_abbr')[0]
for k, v in meta_values.iteritems():
meta_values[k] = v.encode('utf8')
bundle.data['meta'] = meta_values
return bundle
这正是我想要它做的,但它确实是一团糟
请注意,我将字段的内容拉出来,并以对象形式将某些字段保存到“元”字段中。这是因为员工记录可能会随着时间的推移而改变,我希望在创建此记录时获得该记录的快照
有没有一种更直接的方法可以引用和提取资源的内容以分配给这个“元”字段