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
但我明确地验证单元领域。(至少我这样认为)
我究竟做错了什么?