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_by = models.CharField(blank=True,max_length=20,editable=False)
This works, but on update the updated_by field is set to the User ID
instead of the User Name.
Also, none of the 4 audit fields are displayed in the browse/list and
change forms. I need to display them read-only on both.
Any ideas on those 2 issues? I think this thread will help a lot of
struggling newbies like me.
Thanks, Lee
On Jun 1, 12:08 am, Ryan <ryan.osb...@live.co.uk> wrote:
> 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.Model):
> created = models.DateTimeField(auto_now_add=True)
> created_by = models.ForeignKey(User, editable=False)
> updated = models.DateTimeField(auto_now=True)
> updated_by = models.ForeignKey(User, editable=False)
>
> class Meta:
> abstract = True
>
> class Entity1(AuditedTable):
> title = models.CharField(max_length=20)
>
> def __unicode__(self):
> return self.title
>
> admin.py
> -------------
> from django.contrib import admin
> from app.models import Entity1
>
> class AuditAdmin(admin.ModelAdmin):
> def save_model(self, request, obj, form, change):
> if change:
> obj.updated_by = request.user
> else:
> obj.created_by = request.user
> obj.save()
>
> class Entity1Admin(AuditAdmin):
> pass
>
> admin.site.register(Entity1, Entity1Admin)
>
> Hope that helps,
>
> Ryan
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment