Skip to content Skip to sidebar Skip to footer

Why Is My Overridden Save Method Not Running In My Django Model?

I have this model class Clinic(models.Model): name = models.CharField(max_length=100) email = models.EmailField(blank=True) website = models.URLField(blank=True) ph

Solution 1:

Try this one:

def save(self, *args, **kwargs):
    self.slug = slugify(self.name)
    super(Clinic, self).save(*args, **kwargs)

But if you want to populate slug only once on creation:

def save(self, *args, **kwargs):
    if not self.pk:
        self.slug = slugify(self.name)
    super(Clinic, self).save(*args, **kwargs)

Solution 2:

In your save() method you assign the result of the call to slugify to a local variable, not to your instance's slug attribute. just replace slug with self.slug and it will work.


Post a Comment for "Why Is My Overridden Save Method Not Running In My Django Model?"