Login

Memento

Author:
manelvf
Posted:
January 8, 2011
Language:
Python
Version:
1.1
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. Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
  5. Help text hyperlinks by sa2812 1 year, 8 months ago

Comments

Please login first before commenting.