Reputation: 15563
In my django project there's a Blog model which I'm willing to create a slug for it's title:
class Blog(models.Model):
title = models.CharField(default='', max_length=100, verbose_name=u'عنوان')
slug = models.SlugField(max_length=100, allow_unicode=True)
# other stuffs
def save(self, *args, **kwargs):
self.slug = slugify(self.title, allow_unicode=True)
super(Blog, self).save(*args, **kwargs)
def __str__(self):
return self.slug
In django admin I don't fill slug field and when I hit the save button it says:
This field is required.
Isn't my code suppose to create slug automatically? Is there something else I should do?
Upvotes: 0
Views: 1232
Reputation: 903
You should set blank=True
for your slug field. This way it won't be required and it will be set to slugified title when save method will run.
class Blog(models.Model):
title = models.CharField(default='', max_length=100, verbose_name=u'عنوان')
slug = models.SlugField(max_length=100, allow_unicode=True, blank=True)
Upvotes: 1