~linuxgoose/bocpress

579344665340eef72287ea44400d2d5e4f0741fb — Jordan Robinson 2 months ago 24df4b0
add normalisetags management command
1 files changed, 15 insertions(+), 0 deletions(-)

A main/management/commands/normalisetags.py
A main/management/commands/normalisetags.py => main/management/commands/normalisetags.py +15 -0
@@ 0,0 1,15 @@
# main/management/commands/normalize_tags.py
from django.core.management.base import BaseCommand
from main.models import Post

class Command(BaseCommand):
    help = "Normalize tags on all posts"

    def handle(self, *args, **options):
        posts = Post.objects.all()
        for post in posts:
            if post.tags:
                cleaned_tags = [t.strip() for t in post.tags.split(",") if t.strip()]
                post.tags = ",".join(cleaned_tags)
                post.save()
        self.stdout.write(self.style.SUCCESS("All posts tags normalised."))