Django Autoslug raise ValueError("Cannot serialize function: lambda")

Viewed 29

Am trying to create a custom slug for a blog post, and am using django AutoSlug to generate the slug automatically but am getting ths error.

 raise ValueError("Cannot serialize function: lambda")
 ValueError: Cannot serialize function: lambda

This is my models.py

from autoslug import AutoSlugField
class Blog(models.Model):
    ....
   category = models.ForeignKey(Category, 
              on_delete=models.CharField, null=True, 
              blank=True)
   tag = models.ManyToManyField(Tag, blank=True)
   title = models.CharField(max_length=255)
   pub_date = models.DateTimeField(auto_now_add=True)
   slug = AutoSlugField(populate_from=lambda instance: 
                    instance.title,
                     unique_with=['category', 'tag', 
                    'pub_date__month'],
                     slugify=lambda value: 
                     value.replace(' ', '-'),
                     null=True
                     ) 
   .....

I used the example given in the documentation but still am getting the error. I know it can also be done as below in its simplest form

slug = AutoSlugField(populate_from='title')


   
1 Answers

I solved it by creating the function as follows in my models,py

def custom_slugify(value):
    return slugify(value).replace(' ', '_')

then called it in the slug field as follows

slug = AutoSlugField(populate_from='title',
                     unique_with=['category', 'tag', 'pub_date__month'],
                     slugify=custom_slugify,
                     null=True
                     )
Related