Made the slug variable for futures urls and did ./makemigrations and migrate and the parameter appears in the admin panel, but when I try to migrate after I made an empty "theblog" migration I get this error:
class Migration(migrations.Migration):
File "xxx/theblog/models.py", line 104, in Migration
migrations.RunPython(generate_slugs_for_old_posts, reverse=reverse_func),
TypeError: __init__() got an unexpected keyword argument 'reverse'
changed the slug parameter from null and blank to unique but that doesn't seem to be the problem now. I know that the problem is coming from get_success_url but really don't know how to fix it.
models.py:
from django.utils.text import slugify
class Post(models.Model):
title= models.CharField(max_length=100)
header_image = models.ImageField(null=True , blank=True, upload_to="images/")
title_tag= models.CharField(max_length=100)
author= models.ForeignKey(User, on_delete=models.CASCADE)
body = RichTextUploadingField(extra_plugins=
['youtube', 'codesnippet'], external_plugin_resources= [('youtube','/static/ckeditor/youtube/','plugin.js'), ('codesnippet','/static/ckeditor/codesnippet/','plugin.js')])
post_date = models.DateTimeField(auto_now_add=True)
category = models.CharField(max_length=50, default='uncategorized')
slug = models.SlugField(unique=True)
snippet = models.CharField(max_length=200)
status = models.IntegerField(choices=STATUS, default=0)
likes = models.ManyToManyField(User, blank=True, related_name='blog_posts')
def save(self, *args, **kwargs):
self.slug = self.generate_slug()
return super().save(*args, **kwargs)
def generate_slug(self, save_to_obj=False, add_random_suffix=True):
generated_slug = slugify(self.title)
random_suffix = ""
if add_random_suffix:
random_suffix = ''.join([
random.choice(string.ascii_letters + string.digits)
for i in range(5)
])
generated_slug += '-%s' % random_suffix
if save_to_obj:
self.slug = generated_slug
self.save(update_fields=['slug'])
return generated_slug
def generate_slugs_for_old_posts(apps, schema_editor):
Post = apps.get_model("theblog", "Post")
for post in Post.objects.all():
post.slug = slugify(post.title)
post.save(update_fields=['slug'])
def reverse_func(apps, schema_editor):
pass # just pass
class Migration(migrations.Migration):
dependencies = []
operations = [
migrations.RunPython(generate_slugs_for_old_posts, reverse=reverse_func),
]