Re: Auditing Record Changes in All Admin Tables
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 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/-/RDk4VnBBdU1mck1K. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Auditing Record Changes in All Admin Tables
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=20,editable=False) updated = models.DateTimeField(auto_now=True) updated_by = models.CharField(blank=True,max_length=20,editable=False) class Meta: abstract = True class Entity1(AuditedTable): title = models.CharField(max_length=20) ... admin.py --- from myapp.models import Entity1 from django.contrib import admin from django.db import models class AuditAdmin(admin.ModelAdmin): def save_model(self, request, obj, form, change): if change: obj.updated_by = request.user.username else: obj.created_by = request.user.username obj.save() class Entity1Admin(AuditAdmin): list_display = ('title','created','created_by','updated','updated_by') readonly_fields = ('created','created_by','updated','updated_by') admin.site.register(Entity1,Entity1Admin) Thanks again for the excellent help 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Auditing Record Changes in All Admin Tables
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 post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Auditing Record Changes in All Admin Tables
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 just need to figure out why "obj.updated_by = request.user" stuffs the user ID instead of the user name, and this solution will be good to go. -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Auditing Record Changes in All Admin Tables
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_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 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Auditing Record Changes in All Admin Tables
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 view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/NV9uTUJxUVRkTW9K. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Auditing Record Changes in All Admin Tables
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) updated = models.DateTimeField(auto_now=True) updated_by = models.CharField(blank=True,max_length=20) 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): inlines = (JoinTableInline,) admin.site.register(Entity1,Entity1Admin) - I inserted a sys.exit() line to dump request.user, and it has the correct value, but it's not getting pushed to the database record. Thanks very much for your help, do you have other ideas? -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Auditing Record Changes in All Admin Tables
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 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Auditing Record Changes in All Admin Tables
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_now=True) updated_by = models.CharField(blank=True,max_length=20) class Meta: abstract = True def save_model(self, request, obj, form, change): if change: updated_by = request.user else: created_by = request.user class Entity1(AuditedTable): title = models.CharField(max_length=20) def __unicode__(self): return self.title The initial record creation works great, but on update, the user ID is returned by request.user instead of the user name. Anybody have ideas how to fix that? On May 29, 12:37 am, Ryan wrote: > This will show you how to achieve this in the django > admin:https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contr... -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Re: Auditing Record Changes in All Admin Tables
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 django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Auditing Record Changes in All Admin Tables
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 that the user name is not available in many contexts. This seems basic for database apps, but I'm not finding a solution. Anyone get this to work, and where did you define the subclass? Thanks for any help or ideas. -- 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+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.