create_pattern_examples.py
829 Bytes
# -*- coding: utf-8 -*-
from django.core.management.base import BaseCommand
from django.db.transaction import atomic
from dictionary.models import Gender
from patterns.models import Pattern, PatternExample
class Command(BaseCommand):
help = 'Creates pattern examples'
def handle(self, **options):
create_examples()
@atomic
def create_examples():
for pattern in Pattern.objects.all():
gender_ids = pattern.inflection_set.values_list(
'gender', flat=True).distinct()
for gender_id in gender_ids:
if gender_id is not None:
existing_example = PatternExample.objects.filter(
pattern=pattern, gender_id=gender_id)
if not existing_example:
pattern.create_example(Gender.objects.get(id=gender_id))