remove_subexpressions.py 899 Bytes
from django.core.management.base import BaseCommand

from webapp.models import Expression


class Command(BaseCommand):
    help = 'Remove subexpressions from database.'

    def handle(self, *args, **options):
        remove_subexpressions()


def remove_subexpressions():
    removed_count = 0
    print 'all before:', Expression.objects.count()
    print 'main before:', Expression.objects.filter(main_expression=None).count()
    print 'sub before:', Expression.objects.exclude(main_expression=None).count()

    for expression in Expression.objects.exclude(main_expression=None):
        expression.delete()
        removed_count += 1

    print 'removed:', removed_count
    print 'remaining:', Expression.objects.count()

    print 'remaining main:', Expression.objects.filter(main_expression=None).count()
    print 'remaining sub:', Expression.objects.exclude(main_expression=None).count()