在Django中,如何覆盖“保存并继续”功能? [英] In Django, how to override the 'Save and continue' feature?
本文介绍了在Django中,如何覆盖“保存并继续”功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要在ModelAdmin中添加一些保存前和保存后的逻辑,但是仅当用户通过保存并继续编辑按钮而非保存按钮提交表单时。我该怎么做?
I need to add some pre- and post-save logic to my ModelAdmin, but only when the user submitted the form via the 'Save and continue editing' button and not the 'Save' button. How can I do this?
推荐答案
就像覆盖普通的保存方法一样,您需要覆盖您的ModelAdmin中的save_model()
函数,其中包括请求 object
。从请求
对象,您可以获取 POST
对象,该对象将包括'_ continue'
键,如果用户单击了保存并继续按钮。示例:
Just like overriding the normal save method, you need to override the save_model()
function in your ModelAdmin, which includes the request object
. From the request
object you can get the POST
object, which will include a '_continue'
key if the user clicked the 'Save and continue button'. Example:
class MyAdmin(admin.ModelAdmin):
def save_model(self, request, obj, form, changed):
if '_continue' in request.POST:
# add your code here
return super(ServerAdmin, self).change_view(request, obj, form, changed)
这篇关于在Django中,如何覆盖“保存并继续”功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文