简体   繁体   English

检索记录中已更新的字段

[英]Retrieve the fields that have been updated in a record

Can I retrieve the fields that have been updated in a view? 我可以检索视图中已更新的字段吗? Ideally I would be able to retrieve the total number of fields that have been updated (item added to list field or field changed) and the names/count of the individual fields. 理想情况下,我将能够检索已更新的字段总数(添加到列表字段或更改的字段的项目)以及各个字段的名称/数量。

Given a db record such as: 给定一个数据库记录,例如:

{
    "_id": "e3cfb5e19e5c05fb6822c5ee228c4f2e",
    "_rev": "52-c78a26776b0e0736dd45e44a47634031",
    "last_updated": "23/04/2015",
    "title", "testdoc",
    "samples": [
        {
            "confidence": "high",
            "handle": "joetest"
        }
    ],
    "locations": [
        {"Toronto", "Ontario"},
        {"Vancouver", "British Columbia"}
    ]
}

If there are 2 new items in "samples" and 1 in "locations" I would see an updated count of 3. 如果“样本”中有2个新项目,而“位置”中有1个新项目,则更新后的计数为3。

Assuming you retrieve the data before it's updated, you can compare it to the data after it's updated. 假设您在更新之前检索数据,则可以将其与更新之后的数据进行比较。 The following simple function looks at any keys with arrays in the original data and returns the length of the new data. 以下简单函数查看原始数据中具有数组的任何键,并返回新数据的长度。

def list_delta(before, after):
    out = {}

    for key in before:
        if isinstance(before[key], list):
            out[key] = len(after[key]) - len(before[key])

    return out

before = {'name': 'davidism', 'numbers': [0, 1, 2, 3, 4]}
after = do_update(before)
# after = {'name': 'davidism', 'numbers': [0, 1, 2, 3, 4, 5]}
list_delta(before, after)
# returns {'numbers': 1}

You can get more clever and compare if keys were added or removed, or if the actual values changed, or do a deeper recursive comparison for nested structures, but that's beyond the scope of this answer. 您可以更聪明地进行比较,以比较是否添加或删除了键,或者实际值是否发生了变化,或者对嵌套结构进行了更深入的递归比较,但这超出了此答案的范围。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python访问具有Defualt值已更新的值的字典 - Python Accessing Dict That Has Defualt Values That Have Been Updated 格式错误的日志消息; 日志记录已写入日志 - bad formatted logging message; the log record have been written to the log Django Model Formset:仅跟踪对已在集合中更新/保存的项目的更改? - Django Model Formset: only track changes to those items that have been updated/saved in the set? 在已更新的 Counter() 对象上使用 pprint 的 TypeError(边缘情况的位) - TypeError using pprint on Counter() objects that have been updated (bit of an edge case) 如何记录QTreeView项已被双击? - How can I record that QTreeView items have been double-clicked? Pydantic 在字段中创建和更新 - Pydantic created at and updated at fields Model 字段未更新 - Model fields not updated Django:updatedat / created_at字段的TZ信息“太”精确了 - Django: updated_at/created_at fields have TZ info are “too” precise 在已添加到现有 model 的产品 model 字段上运行 makemigrate 时出错 - Getting an Error when running makemigrate on Product model fields that have been added to an existing model 检索选定的记录 - Retrieve the selected record
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM