django reversion and get_<field>_display

124 Views Asked by At

i have a model like this:

USER_TYPES=(("pro","Professional"),("beg","Beginner"))
mdl_user(models.Model):
    name=models.CharField(max_length=128)
    utype=models.CharField(max_length=3,Choices=USER_TYPES)

i want track user changes using django-reversion

on display changes using default example

from reversion.helpers import generate_patch_html
generate_patch_html(old_version, new_version, "utype", cleanup="semantic")

return

<span>pro<ins>beg</ins></span>

but i want return Choices values like this:

<span>Professional<ins>Beginner</ins></span>
1

There are 1 best solutions below

0
On

i found there is no such things for django reversion

i use this helper to get display of choices

def replace_choices_value(req_mdl, objs):
    listobj = []
    fields = req_mdl._meta.get_fields()
    if type(objs) != list:
        objs = [objs, ]

    for obj in objs:
        newobj = {}
        for field in fields:
            display_method = "get_%s_display" % field.name
            if hasattr(req_mdl, display_method):
                newobj[field.name] = dict(field.flatchoices).get(obj.field_dict[field.name])
            else:
                newobj[field.name] = obj.field_dict.get(field.name, "")
        listobj.append(newobj)
    return listobj