Skip to content Skip to sidebar Skip to footer

Cannot Change Model Instance After A Validationerror Is Thrown In Django Admin Area

Imagine a model like this: class CFile(models.Model): filepath = models.FileField(upload_to=...) collection = models.ForeignKey('FileCollection',null=True) ... # other a

Solution 1:

I think the clearest way is to declare another field in your model for filename and make it unique for every collection. Like this:

class CFile(models.Model):
   filepath   = models.FileField(upload_to=...)
   collection = models.ForeignKey("FileCollection",null=True, related_name='files')
   filename = models.CharField(max_length=255)
   ... # other attributes that are not relevant

    class Meta:
        unique_together = (('filename', 'collection'),)

    def save(self, *args, **kwargs):
        self.filename = bname(self.filepath.path)
        super(CFile, self).save(args, kwargs)

Post a Comment for "Cannot Change Model Instance After A Validationerror Is Thrown In Django Admin Area"