Django ModelForm:如何覆盖或检查“save_as” [英] Django ModelForm: How to override or check for "save_as"
问题描述
我需要添加一些自定义,例如Django Admin中的一个实例是saved_as一个副本。可以在 save()
中执行此操作,如果是,请检查 save_as kwarg
?
或者有一个 save_as()
方法我可以覆盖某处。我无法找到任何关于常规 save()
与 save_as = True $ c $之间的过程差异的信息c。。
I need to add some customizations for the case that a instance in Django Admin is "saved_as" a copy. Can I do that in save()
, if so how do I check for the save_as kwarg
?
Or is there a save_as()
method I can override somewhere. I was not able to find any information about the differences in the process between a regular save()
and one with save_as = True
.
有人可以给我一个例子,或给我一个提示去哪里?
Can someone point me to an example or give me an hint where to go?
谢谢你非常喜欢你的时间!
Thank you very much for your time!
:)
推荐答案
a href =http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view =nofollow> http://docs.djangoproject.com /en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.change_view
另存为新按钮是一个提交元素,一个特定的名称 _saveasnew
,这意味着您可以通过 _saveasnew
POST参数的存在来检查该按钮是否被按下。
The Save as New button is a submit element with a specific name, _saveasnew
, meaning you can check whether that button was pressed via the presence of the _saveasnew
POST parameter.
def change_view(self, request, object_id, extra_context=None):
if '_saveasnew' in request.POST:
# custom logic for save as new
print "I am saved as new"
return super(ModelAdmin, self).change_view(request, object_id, extra_context)
这篇关于Django ModelForm:如何覆盖或检查“save_as”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!