从包django-reversion的field_dict中过滤特定的字段名称和值

时间:2015-03-05 15:54:02

标签: json django django-reversion

我有一个函数,它从版本的reversion.models返回json数据作为历史记录。

from django.http import HttpResponse
from reversion.models import Version
from django.contrib.admin.models import LogEntry
import json
def history_list(request):
    history_list = Version.objects.all().order_by('-revision__date_created')
    data = []
    for i in history_list:
        data.append({
            'date_time': str(i.revision.date_created),
            'user': str(i.revision.user),
            'object': i.object_repr,
            'field': i.revision.comment.split(' ')[-1],
            'new_value_field': str(i.field_dict),
            'type': i.content_type.name,
            'comment': i.revision.comment
        })

    data_ser = json.dumps(data)
    return HttpResponse(data_ser, content_type="application/json")

当我运行上面的代码片段时,我得到输出json为

[{"type": "fruits", "field": "colour", "object": "anyobject", "user": "anyuser", "new_value_field": "{'price': $23, 'weight': 2kgs, 'colour': 'red'}", "comment": "Changed colour."}]

从上面的功能

'comment': i.revision.comment

将json返回为"评论":"更改颜色"和颜色是我在函数中编写的字段,用于从注释中检索它

'field': i.revision.comment.split(' ')[-1]

但我认为从field_dict获取fieldname和value是一种更好的方法

问题:从上面的json列表中我想过滤new_field_value和old_value。仅在new_filed_value中的颜色值。

1 个答案:

答案 0 :(得分:1)

获取更改的字段并不像检查注释那么容易,因为这可以被覆盖。

Django-reversion只需处理存储每个版本,而不是比较。

您最好的选择是查看django-reversion-compare module及其admin.py代码。

其中的大部分代码都是为了生成一个简洁的并排HTML差异页面,但代码应该可以重新用于生成每个对象的已更改字段列表(因为可以每个版本不止一个更改字段。)


代码应该 * 包含一个独立于视图的方式来获取更改的字段,但这应该让你开始:

from reversion_compare.admin import CompareObjects
from reversion.revisions import default_revision_manager

def changed_fields(obj, version1, version2):
    """
    Create a generic html diff from the obj between version1 and version2:
        A diff of every changes field values.
    This method should be overwritten, to create a nice diff view
    coordinated with the model.
    """
    diff = []

    # Create a list of all normal fields and append many-to-many fields
    fields = [field for field in obj._meta.fields]
    concrete_model = obj._meta.concrete_model
    fields += concrete_model._meta.many_to_many

    # This gathers the related reverse ForeignKey fields, so we can do ManyToOne compares
    reverse_fields = []
    # From: http://stackoverflow.com/questions/19512187/django-list-all-reverse-relations-of-a-model
    changed_fields = []
    for field_name in obj._meta.get_all_field_names():
        f = getattr(
            obj._meta.get_field_by_name(field_name)[0],
            'field',
            None
        )
        if isinstance(f, models.ForeignKey) and f not in fields:
            reverse_fields.append(f.rel)

    fields += reverse_fields

    for field in fields:
        try:
            field_name = field.name
        except:
            # is a reverse FK field
            field_name = field.field_name

        is_reversed = field in reverse_fields
        obj_compare = CompareObjects(field, field_name, obj, version1, version2, default_revision_manager, is_reversed)

        if obj_compare.changed():
            changed_fields.append(field)

    return changed_fields

然后可以这样调用:

changed_fields(MyModel,history_list_item1, history_list_item2)

history_list_item1history_list_item2对应各种实际Version项。

*:作为贡献者,我会坚持下去。