在 Django 的 simple_history 中使用 diff_against 时如何指定 "excluded_fields"

how to specify "excluded_fields" when using diff_against in simple_history in Django

我在 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”。这些是我尝试过的方法,但 none 有效:

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'))

这很可能很简单,但我还没弄明白。任何帮助都会很棒。 谢谢。

你应该像这样使用它:

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

diff_against 接受关键字参数 excluded_fields 而不是 excluded_fields 作为 HistoricalRecords class.

的方法