save()
方法應該返回對象,無論是否保存。它更應是這樣的:
def save(self, commit=True):
input_name = self.cleaned_data.get('input_name')
if input_name == "MyKeyword":
commit = False
return super().save(commit=commit)
然而,正如你可以看到here和here,形式已經被稱爲與commit=False
和對象是由您ModelAdmin
的save_model()
方法後保存。這就是你得到AttributeError 'NoneType' object has no attribute 'save'
的原因。如果你檢查異常的回溯,我很確定這個錯誤來自this line。
因此,你應該,另外,覆蓋您ModelAdmin
的save_model()
方法:
def save_model(self, request, obj, form, change):
if form.cleaned_data.get('input_name') == 'MyKeyword':
return # Don't save in this case
super().save_model(request, obj, form, change)
# And you'll also have to override the `ModelAdmin`'s `save_related()` method
# in the same flavor:
def save_related(self, request, form, formsets, change):
if form.cleaned_data.get('input_name') == 'MyKeyword':
return # Don't save in this case
super().save_related(request, form, formsets, change)
鑑於此回答您的評論
It is working now but I just have one related question. The admin site will still display a green banner at the top saying "The model was added successfully". Can I override some method so that it is red and says "The model already exists"?
它看起來就是你想要的要做的不是重寫保存方法,而是controlling form validation。這是完全不同的。您只需重寫表單的乾淨方法。
from django import forms
def clean(self):
"""
super().clean()
if self.cleaned_data.get('input_name') == 'MyKeyword':
raise forms.ValidationError("The model already exists")
,或者甚至更好,因爲它看起來像你想clean a single field:
from django import form
def clean_input_name(self):
data = self.cleaned_data['input_name']
if data == 'MyKeyWord':
raise forms.ValidationError("The model already exists")
然而,我猜input_name
也是模型的一個領域,你不想提這個僅在一個表單上出現錯誤,但是在整個項目中出錯在這種情況下,你在找什麼是Model validators:
from django.core.exceptions import ValidationError
def validate_input_name(value):
if value == 'MyKeyword':
raise ValidationError("The model already exists")
那麼這個代碼有什麼問題?你可以簡化它改變條件爲'if input_name!=「MyKeyword」:super(MyCustomForm,self).save(commit = commit)',但除此之外它應該像你寫的那樣工作。 – xbello
我試過,但錯誤是:AttributeError'NoneType'對象沒有'保存'屬性 – Rueen1963
然後,也許你的ModelForm沒有返回被保存的對象:它返回「None」。閱讀這個答案http://stackoverflow.com/a/817364/1688590。 – xbello