No problem.
One final piece of advice would be to move the readonly_fields out of
Entity1Admin and into AuditAdmin. That way you only have to define it once and
inherit from it as with all the other options rather than re-defining it for
each new model.
Ryan
--
You received this message bec
Bingo! Everything's working. Here's the complete solution for others:
models.py
-
from django.db import models
from django.contrib import admin
class AuditedTable(models.Model):
created = models.DateTimeField(auto_now_add=True)
created_by =
models.CharField(blank=True,max_length=2
That bit is the easy bit :p
Just change request.user to request.user.username
Ryan
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To view this discussion on the web visit
https://groups.google.com/d/msg/django-users/-/RXpUd1BTX2E3RVVK.
To po
Found the solution to display the audit fields in the change list and
change form -- add these lines in admin.py:
class Entity1Admin(AuditAdmin):
list_display =
('title','created','created_by','updated','updated_by')
readonly_fields = ('created','created_by','updated','updated_by')
So now I j
Ryan- that helps a ton, thanks much. I don't think created_by and
updated_by should be foreign keys, because a user can be deleted but
all of his record adds/updates need to persist. So I replaced those
two lines with:
created_by = models.CharField(blank=True,max_length=20,editable=False)
updated_
The model code still needs to stay in the model, it is just the save_model
method had to be moved to the admin.py. I think the following is what you
are looking for:
models.py
---
from django.db import models
from django.contrib.auth.models import User
class AuditedTable(models.Mod
Moved it to admin.py but it ignores the auto_now parameters (leaving
timestamps blank) and doesn't populate the created_by/updated_by
fields either:
class AuditAdmin(admin.ModelAdmin):
created = models.DateTimeField(auto_now_add=True)
created_by = models.CharField(blank=True,max_length=20)
u
That code is supposed to go in the models admin definition like so:
admin.py:
--
class AuditAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.user = request.user
obj.save()
Ryan
--
You received this message bec
Thanks for that tip Ryan. Here's the relevant models.py code I used to
get this to _almost_ work:
-
class AuditedTable(models.Model):
created = models.DateTimeField(auto_now_add=True)
created_by = models.CharField(blank=True,max_length=20)
updated = models.DateTimeField(auto_n
This will show you how to achieve this in the django admin:
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_model
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to
All of my tables have 4 audit fields:
created (timestamp)
created_by (user name)
updated (timestamp)
updated_by (user name)
I'd like to define a subclass of models.Model that sets these fields
on create/update, and then make all my models of that subclass -- but
all the related posts I see imply
11 matches
Mail list logo