Manipulating Data in Django's Admin Panel on Save
From the Django Docs:
ModelAdmin.save_model(self, request, obj, form, change)
The
save_model
method is given theHttpRequest
, a model instance, aModelForm
instance and a boolean value based on whether it is adding or changing the object. Here you can do any pre- or post-save operations.For example to attach
request.user
to the object prior to saving:
from django.contrib import admin
class ArticleAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, change):
obj.user = request.user
super().save_model(request, obj, form, change)
class ArticleAdmin( admin.ModelAdmin ):
fields = [ 'title', 'body' ]
form = ArticleAdminForm
def save_model(self, request, obj, form, change):
obj.author = "name Here"
obj.save()