model save memos

 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. Collapsed stacked inlines by Aneon 4 years, 11 months ago
  2. Admin page without inlines dinamically by davmuz 1 year, 10 months ago
  3. keeping Model and Field History (everywhere) by buriy 5 years, 7 months ago
  4. Inline OneToOne relations in django-admin by bjourne 3 years, 11 months ago
  5. Creator/updater fields for admin by baumer1122 5 years, 9 months ago

Comments

(Forgotten your password?)