update_entries.py 748 Bytes
from django.core.management.base import BaseCommand

from webapp.models import Entry, Expression


class Command(BaseCommand):
    help = 'Update Entry objects.'

    def handle(self, *args, **options):
        remove_entries()
        add_entries()


def remove_entries():
    Entry.objects.all().delete()


def add_entries():
    catchwords = Expression.objects.filter(is_catchword=True).order_by('text')
    for expr in catchwords:
        print (expr.text)
        entry, created = Entry.objects.get_or_create(name=expr.text)

        domains = expr.meaning.domains.all()
        meaning = expr.meaning
        source = expr.link.source

        entry.domains.add(*domains)
        entry.meanings.add(meaning)
        entry.sources.add(source)