我已經在我的Django應用程序以下型號:DoesNotExist()
class Property(models.Model):
...various attributes...
class Booking(models.Model):
property = models.ForeignKey(
Property
)
...more attributes...
我實現了Booking
,檢查一個Booking
的各種制約因素的clean()
方法。許多這些限制取決於Booking
和Property
的關聯。出於這個原因,我已在在Booking
乾淨()方法如下:
if self.property is not None:
...Property related validations...
一旦在管理應用進入一個新的預訂和公正的Save按鈕,而無需輸入任何東西,這會產生上self.property is not None
一個DoesNotExist exception
。我的理解是,丟失的關聯應該由Django的單個字段驗證捕獲。
當我註釋掉clean()
方法時,上述情況不會發生,當我提交空白預訂表單時,將缺失的關聯標記爲錯誤。
我顯然失去了一些東西,但沒有得到什麼線索......
你有沒有試過改變你的條件爲'如果hasattr(self,'property')和self.property不是None'? – mVChr
@mVChr謝謝,解決了它。我必須說,一個非常微妙的解決方案,說實話,它背後的原因逃脫了我。這是特定的Django還是Python?如果你願意,把它添加爲答案,我會接受它。 – Roger