阅读背景:

在django管理区域中抛出ValidationError后,无法更改模型实例

来源:互联网 

Imagine a model like this:

想象一下这样的模型:

class CFile(models.Model):
   filepath   = models.FileField(upload_to=...)
   collection = models.ForeignKey("FileCollection",null=True)
   ... # other attributes that are not relevant

   def clean(self):
     bname = os.path.basename
     if self.collection:
       cfiles = self.baseline.attachment_set.all()
       with_same_basename = filter(lambda e: bname(e.filepath.path) == bname(self.filepath.path),cfiles)
       if len(with_same_basename) > 0:
         raise ValidationError("There already exists a file with the same name in this collection")   

class FileCollection(models.Model):
  name = models.CharField(max_length=255)
  files= models.ManyToManyField("CFile")
class



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: