【问题标题】:On the fly modification of Django QuerySet objects动态修改 Django QuerySet 对象
【发布时间】:2015-08-19 21:32:47
【问题描述】:

我正在尝试为查询集中的每个对象添加两个属性,并为特定子集再添加一个:

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

当我为每个对象添加一个显示名称并基于条件添加一个 immediate_parent 属性时,修改工作。当我尝试将 is_parent 属性设置为已被识别为子对象的对象时,就会出现问题,因为每个对象在迭代中都标识其父对象,因此通过拉取它们的 id 来识别它们。

在 shell 中,我尝试在循环中打印 parent 对象,它具有 is_parent 属性,但是当我在循环外查看 causelist_data 时,它同时具有 display_name 和 immediate_parent 属性。我还尝试在过滤器(parent = causelist_data.filter(cause_id=c.immediate_parent).update(is_parent=True))之后更新父对象,但看起来我无法添加这样的属性,只能更新。

我在这里遗漏了什么吗?我的查找不正确吗?

【问题讨论】:

  • 修改我创建的parent对象不会修改原来的QuerySet。我想出了一种方法,通过对 QuerySet 进行 3 次迭代,但这并不漂亮。如果您有建议,我很乐意在我刚刚发布的 code review question 上看到它们。
  • 一个查询集在你获取它的一部分或开始迭代它之后就不能被修改。

标签: python django django-queryset


【解决方案1】:

我认为在所有情况下您都会发出另一个查询 更好地控制您的filters,您应该会没事的。

filters = {cause_set_version_id:cause_set_version_id}

causelist_data = CauseHierarchy.objects.filter(filters**
).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]

    filters['cause_id'] = c.immediate_parent
    parent = CauseHierarchy.objects.filter(filters**)[0]

    parent.is_parent = True

【讨论】:

    猜你喜欢
    • 2020-10-31
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-18
    • 2018-12-15
    • 2021-08-05
    相关资源
    最近更新 更多