python - Django History for Custom Dashboard -


i have decided use django-simple-history building history of models. in turn using build dashboard. have run bit of snag though. i'm trying output [user] [added,changed, deleted] [object] on/at [time] can't figure out life of me.

so far able display historical record on template can't access else, missing something?

i hoping knowledge of simple history can help, since couldn't hold of author.

here code snippets have far.

models.py

from simple_history.models import historicalrecords  class project(django.db.models.model): ... history = historicalrecords() 

views.py

@login_required def addtmproject(request): user = request.user if request.method == 'post':     form = timematerialsform(request.post)     if form.is_valid():         project = form.save(commit=false)         project.created_by = request.user         today = datetime.date.today()         project.pre_quote = "%s-" % (str(today.year)[2:4])         project.quote = project.objects.latest().quote+1         project.save()         project.history.all()         ... 

and have passed on dashboard/views.py have access it.

@login_required def view_dash(request):     today = datetime.date.today()     user = request.user      proj_perm = user.has_perm('project.add_project')     project = project.objects.all().order_by('-proj_name')     query = project.objects.all().order_by('-id')[:5]     que_quotes = project.objects.filter(status__value__exact = 'quote')     expired = followup.objects.filter(next_followup__lte=today).order_by('next_followup').filter(archived=false)     log = logentry.objects.select_related().all().order_by("-id")     hist = project.history.all() return render_to_response('dashboard/home.html', {'user': user, 'project': project, 'query':query, 'que_quotes':que_quotes, 'expired':expired,                                                   'proj_perm':proj_perm, 'log': log, 'hist':hist,}, context_instance=requestcontext(request)) 

and snippet template. right now, {{ h }} shows "testing simple records of 2011-04-29 10:43:57" on template

home.html

{% if user.is_authenticated %} <div id="large_box">     <h5>activity</h5>         {% h in hist %}         <ul>             <li>{{ h }}</li>         </ul>         {% endfor %} 

if or point me more in depth documentation, great!

thanks everyone!

django-simple-history creates model (and associated database table) mirrors object tie , adds 4 additional fields: history_id, history_date, history_type, , history_object.

history_id: standard primary key

history_date: datetime when change occurred

history_type: 1 of +, ~, -. (+ means added, ~ means changed, , - means deleted)

history_object: representation of model history being stored for

so, @ basic level, can "[added,changed, deleted] [object] on/at [time]" in output, using effect of:

{{ h.history_type }} {{ h.history_object }} on/at {{ h.history_date }} 

you'll want create template tag or convert +, ~, , - more understandable, 'created', 'changed', 'deleted'. {{ h.history_object }} should return object's __unicode__, i'm assuming, might need make modifications there or return {{ h.history_object.__class__ }} or {{ h.history_object._meta.verbose_name }}, instead. (not sure if work in practice though.) and, of course, can apply date filter {{ h.history_date }} make format want.

getting user more difficult. django-simple-history doesn't seem store data, there's no record of user made modification. however, since duplicates object existed, away adding modified_by field model , filling request.user pre-save. then, when django-simple-history thing, field copied on rest , available via {{ h.modified_by }}.


Comments