0

我正在寻找使用 frappe.db.delete 删除自定义表仓库位置中最近修改的记录。我想将删除限制为仅与某些过滤器匹配的一条记录。

该表是一个子表,如果这很重要的话。

我不清楚如何根据“修改”值过滤一条记录。我试过了:

frappe.db.delete(‘Warehouse Locations’,
  {“warehouse”: warehouse,
  “parent”: item_code,
  “shelf”: shelf,
  “modified”:("=",last_record_to_keep[0].modified)})

运行上述查询时出现语法错误。

4

2 回答 2

1

我认为@ChillarAnand 回答的解决方案很有帮助。

相反,我想提供一种不同的方法来解决您面临的问题。根据您的问题,目标是从仓库位置(子表)中仅删除一条记录。

    # 1. Get the parent document for the warehouse location.
    parent_doc = frappe.get_doc("Doctype", docname)
    # 2. iterate through the child table rows to find the row meet your filter
    # and assign to row_to_detele for late use or you can delete straight away
    row_to_delete = ""
    for row in parent_doc.warehouse_locations:
        if row.modified == last_record_to_keep[0].modified:
            row_to_delete = row.name
            break

    # 3. to remove the child table from the parent doc method
    parent_doc.remove(row_to_delete)
    

的文档parent_doc.remove(),可以通过以下github路径找到:https ://github.com/frappe/frappe/blob/6b91ade73c07dc1c070ed137cf54a29a3e7b0993/frappe/model/base_document.py#L210 (2021年10月7日)

于 2021-08-28T00:24:13.380 回答
0

首先,通过运行过滤掉使用ORM删除的记录

record = frappe.get_list('Warehouse Locations', order_by='-modified')[0]

过滤掉它后,您可以使用 frappe.db.delete 将其删除。

frappe.db.delete('Warehouse Locations', record)
于 2021-08-23T05:31:03.130 回答