我尝试向queryset中的每个对象添加两个属性,并向特定子集添加一个属性:
causelist_data = CauseHierarchy.objects.filter(cause_set_version_id=cause_set_version_id
).extra(order_by=['sort_order']
).prefetch_related('cause_id','parent_id')
for c in causelist_data:
# adding this attribute works
c.display_name = c.cause_id.cause_name + " (" + c.cause_id.acause + ")"
if len(c.path_to_top_parent.split(',')) == 1:
# adding this attribute works as well
c.immediate_parent = c.path_to_top_parent.split(',')[-1]
else:
c.immediate_parent = c.path_to_top_parent.split(',')[-2]
# this is the problem
parent = causelist_data.filter(cause_id=c.immediate_parent)[0]
parent.is_parent = True
当我为每个对象添加一个显示名,以及基于条件添加一个立即父属性时,修改就起作用了。当我试图设置一个is\u parent属性给那些被标识为子对象的对象时,问题就出现了,当每个对象在迭代中标识其父对象时,我会拉取它们的id。你知道吗
在shell中,我试图打印循环中的parent
对象,它具有is\u parent属性,但是当我查看循环外的causelist\u数据时,它同时具有display\u name和immediate\u parent属性。我还尝试在过滤器(parent = causelist_data.filter(cause_id=c.immediate_parent).update(is_parent=True)
)之后更新父对象,但看起来无法添加这样的属性,只能更新。你知道吗
我是不是漏了什么?我的查找不正确吗?你知道吗
我想在任何情况下你都会提出另一个问题 抓紧你的
filters
你应该会没事的。你知道吗相关问题 更多 >
编程相关推荐