Login

model save memos

Author:
depaolim
Posted:
June 21, 2013
Language:
Python
Version:
1.5
Score:
0 (after 0 ratings)

A simple example to show how to manage an history of memo-on-save. Each time the user saves the model, he must provide a memo message. The full memos history is shown as a read-only tabular-inline. For each memo, user and date are also registered

(Django-Admin-Collapsed-Inlines could be usefull)

 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
#############################################################
# models.py
#############################################################

from django.contrib.auth.models import User
from django.db import models

class Order(models.Model):
    """ sample model """
    code = models.CharField(max_length=30)
    def __unicode__(self):
        return self.code

class OrderNote(models.Model):
    """ model save registration """
    order = models.ForeignKey(Order)
    insert_user = models.ForeignKey(User)
    insert_date = models.DateTimeField(auto_now=True)
    memo = models.TextField()


#############################################################
# admin.py
#############################################################

from django.contrib import admin
from django import forms
from models import *

class OrderNoteInline(admin.TabularInline):
    model = OrderNote
    readonly_fields = ('memo', 'insert_user', 'insert_date')
    extra = 0
    def has_delete_permission(self, request, obj=None):
        return False
    def has_add_permission(self, request, obj=None):
        return False

class OrderForm(forms.ModelForm):
    memo = forms.CharField(widget=forms.Textarea())

class OrderOption(admin.ModelAdmin):
    form = OrderForm
    raw_id_fields = ('items', )
    inlines = [OrderNoteInline]
    def save_model(self, request, obj, form, change):
        OrderNote(order=obj, insert_user=request.user, memo=form.cleaned_data['memo']).save()
        super(OrderOption, self).save_model(request, obj, form, change)

admin.site.register(Order, OrderOption)

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, 7 months ago

Comments

Please login first before commenting.