开发者

django: How to hook save button for Model admin?

I have a Model with a "status" field. When the user users the Admin app to modify an 开发者_StackOverflow社区instance, how to I hook onto the "Save" button click so that I could update "status" to a value that's dependent on the logged in user's username?


Override your modeladmin's save_model-method:

class ModelAdmin(admin.ModelAdmin):       
    def save_model(self, request, obj, form, change):
        user = request.user 
        instance = form.save(commit=False)
        if not change:    # new object
            instance.status = ....
        else:             # updated old object
            instance.status = ...
        instance.save()
        form.save_m2m()
        return instance


Use the pre_save signal. Granted, it will be called on every instance save operation, not only from admin, but it does apply to your situation.


ModelAdmin.save_model() provides just what I need


form.cleaned_data.get('categories')

This gets the value directly in the ModelAdmin.save_model

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜