我必须检查一个名称是否已经在使用,但要这样做,我必须排除我要编辑的项目
class EditItemForm(forms.ModelForm):
name = forms.CharField( max_length=80, min_length=3, required=True )
def clean_name(self):
# Get the name
name = self.cleaned_data.get('name')
if Item.objects.filter(name=name).exclude(id=id).exists():
raise forms.ValidationError('This name is already in use.')
class Meta:
model = Item
fields = ('type', 'name', )
class Item(models.Model):
type = models.IntegerField(choices=TYPE, blank=False, default=NO_CAT)
name = models.CharField(max_length=250 )
def __str__(self): #metodo righiesto
return self.name
如何检索当前项目id/pk以排除它?
暂无答案!
目前还没有任何答案,快来回答吧!