admin.ModelAdmin.save_model()

ModelAdmin.save_model(request, obj, form, change) [source]

The save_model method is given the HttpRequest, a model instance, a ModelForm 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
        obj.save()
doc_Django
2016-10-09 18:33:55
Comments
Leave a Comment

Please login to continue.