0
我有模型RelatedObjectDoesNotExist - 在模型的清潔功能
@with_author
class Lease(CommonInfo):
version = IntegerVersionField()
is_renewed = models.BooleanField(default=False)
unit = models.ForeignKey(Unit)
is_terminated = models.BooleanField(default=False)
def __unicode__(self):
return u'%s %i %s ' % ("lease#", self.id, self.unit)
def clean(self):
model = self.__class__
if self.unit and (self.is_active == True) and model.objects.filter(unit=self.unit, is_terminated = False , is_active = True).count() == 1:
raise ValidationError('Unit has active lease already, Terminate existing one prior to creation of new one or create a not active lease '.format(self.unit))
和我有一個表格
class LeaseForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(LeaseForm, self).__init__(*args, **kwargs)
self.fields['unit'].required = True
class Meta:
model = Lease
fields = [ 'unit',
'is_active','is_renewed', 'description']
,每次保存這種形式不爲單位我收到
選擇價值error相關對象DoesNotExist
從模型我清潔功能,因爲沒有self.unit
但我明確地驗證單元領域。(至少我這樣認爲)
我究竟做錯了什麼?