1

我在 django 的 simple_history 中使用 diff_against。请参阅 simple_history 文档中的“历史差异”: https://django-simple-history.readthedocs.io/en/latest/history_diffing.html 我已经完成了所有工作,但它指出: "diff_against also accepts 2 arguments excluded_fields and included_fields to either explicitly include or exclude fields from being diffed." 我无法弄清楚如何传递这些字段。这是我所拥有的工作(没有任何包含或排除字段):

def historical_changes(qry, id):
    changes = []
    if qry is not None and id:
        last = qry.first()
        for all_changes in range(qry.count()):
            new_record, old_record = last, last.prev_record
            if old_record is not None:
                delta = new_record.diff_against(old_record)
                changes.append(delta)
            last = old_record
    return changes

我使用以下方法在详细视图中调用它:

changes = historical_changes(hpn.history.all(), hpn.pk)

这一切都有效。然后我尝试包含“exclude_field”。这些是我尝试过的,但没有一个奏效:

delta = new_record.diff_against(old_record, excluded_fields('geom'))
or
delta = new_record.diff_against(old_record).excluded_fields('geom')
or
delta = new_record.diff_against(old_record.excluded_fields('geom'))

这很简单,但我没有弄清楚。任何帮助都会很棒。谢谢。

4

1 回答 1

1

你应该像这样使用它:

delta = new_record.diff_against(old_record, excluded_fields=['geom'])

diff_against接受关键字参数excluded_fields而不是类excluded_fields的方法HistoricalRecords

于 2021-11-01T13:00:01.643 回答