Login

Memento

Author:
manelvf
Posted:
January 8, 2011
Language:
Python
Version:
1.1
Tags:
admin register changes persistence tracking
Score:
1 (after 3 ratings)

This class tracks changes in Django Admin. When a save action is performed, it stores the value of the old object using the Memento model.

Example of code for a model in admin.py for a custom 'app':

from app.memento.models import Memento

def save_model(self, request, obj, form, change):
    obj.save()

    data = serializers.serialize("json", [obj, ])
    m = Memento(app="Unidade",model=modelName,data=data, user=request.user)
    m.save()

class UnidadeAdmin(admin.ModelAdmin):
    pass

UnidadeAdmin.save_model = save_model

This stores the former values of 'Unidade' model on 'Memento' model data.

Not tested on previous versions of Django, but could work on them too.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
App: Memento
models.py
"""

from django.contrib.auth.models import User
from django.utils.encoding import smart_unicode
import datetime

from django.db import models
from django import forms


class Memento(models.Model):
    app = models.CharField(max_length=50)
    model = models.CharField(max_length=50)
    data = models.TextField()
    date = models.DateTimeField(auto_now=True)
    user = models.ForeignKey(User)

    def __unicode__(self):
        return self.app + " : " + self.model + " - " + unicode(self.date) + " - " + unicode(self.user)



"""
App: Memento
admin.py
"""

from types import *
from django.contrib import admin
from django.core import serializers
from forestal2.memento.models import Memento


def data_show(obj):
    s = u""
    for o in serializers.deserialize("json",obj.data):
        names = o.object._meta.get_all_field_names()
        for k in names:
            s += unicode(getattr(o.object, k))

    return (u"asdfsads").upper()

data_show.short_description = "Data Show"


class MementoAdmin(admin.ModelAdmin):
    list_display = ('app', 'model', "data_show", 'date','user')
    list_filter = ('app', 'model', 'date','user')

    def data_show(self, obj):
        s = u""
        for o in serializers.deserialize("json",obj.data):
            names = o.object._meta.get_all_field_names()
            print names
            for k in names:
                #if k[0:1] == "_": continue
                #print k
                q = hasattr(o.object, k)
                if q:
                    s += unicode(k) + u": " + unicode(getattr(o.object, k)) + u"<br />"

        return s

    data_show.short_description = "Data Show"
    data_show.allow_tags = True

admin.site.register(Memento, MementoAdmin)
`


views.py: (empty)
tests.py: (empty)

More like this

  1. JSONField by Jasber 6 years ago
  2. Custom model field to store dict object in database by rudyryk 5 years, 1 month ago
  3. Initial values for new items in admin by RommeDeSerieux 5 years, 8 months ago
  4. Group results by a range of values in admin sidebar by wgollino 7 years, 3 months ago
  5. Model Mixin to Save Only Changed Fields by karanlyons 1 year, 9 months ago

Comments

Please login first before commenting.