Django - Store username of logged on user in admin edits
I'm using django as the edit interface for a database, and want to track the last edit. I've got the following class
class CommonInformation(models.Model):
lastEditedOn = models.DateTimeField(auto_now_add=True,null = True, blank=True)
lastEditedby = models.ForeignKey(Person,null = True, blank=True)
class Meta:
abstract = True
which I can use t开发者_如何学Pythono track the edit date, but is there a way to make it automatically store the username of the person logged onto the admin site instead of having to manually select the name each time?
for the admin site you can use save_model in your admin file e.g.
class CommonInformationAdmin(admin.ModelAdmin):
....
def save_model(self, request, obj, form, change):
obj.lastEditedby = request.user
obj.save()
精彩评论