[Fixed]-Django: Attribute error on custom Save()

1👍

You are allowing NULL for the study ForeignKey, so like @karthikr said, you need to handle this gracefully. One way to do this would be:

class Protocol(models.Model):
    study = models.ForeignKey(Study, null=True)
    version = models.DecimalField(max_digits=3, decimal_places=1, null=True)
    slug = models.SlugField(max_length=200, blank=True, null=True)

    def save(self, *args, **kwargs):
        if not self.slug:
            protocolnumber = self.study.protocolnumber if self.study else ""
            self.slug = slugify(protocolnumber)+'-V'+slugify(self.version)
            super(Protocol, self).save(*args, **kwargs)

Leave a comment