Memento

 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 4 years, 11 months ago
  2. Custom model field to store dict object in database by rudyryk 4 years ago
  3. Initial values for new items in admin by RommeDeSerieux 4 years, 7 months ago
  4. Group results by a range of values in admin sidebar by wgollino 6 years, 2 months ago
  5. Model Mixin to Save Only Changed Fields by karanlyons 7 months, 4 weeks ago

Comments

(Forgotten your password?)