Improved performance of update_translation_fields command. Resolves issue 43 (thanks to adamsc).

This commit is contained in:
Dirk Eschler 2010-09-06 08:58:52 +00:00
parent ab03291848
commit 80d97888b0
2 changed files with 12 additions and 12 deletions

View file

@ -11,6 +11,8 @@
ManyToManyField and OneToOneField.
(resolves issue 15)
CHANGED: Improved performance of update_translation_fields command.
(thanks to adamsc, resolves issue 43)
CHANGED: Custom fields are extended instead of overridden.
CHANGED: Factored out settings into a separate settings.py and consistently
used an app specific settings prefix.

View file

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-
from django.conf import settings
from django.db.models import F, Q
from django.core.management.base import (BaseCommand, CommandError,
NoArgsCommand)
@ -9,21 +10,18 @@ from modeltranslation.utils import build_localized_fieldname
class Command(NoArgsCommand):
help = 'Updates the default translation fields of all or the specified' \
help = 'Updates the default translation fields of all or the specified'\
'translated application using the value of the original field.'
def handle(self, **options):
print "Using default language:", DEFAULT_LANGUAGE
for model, trans_opts in translator._registry.items():
print "Updating data of model '%s'" % model
for obj in model.objects.all():
for fieldname in trans_opts.fields:
def_lang_fieldname =\
build_localized_fieldname(fieldname, DEFAULT_LANGUAGE)
#print "setting %s from %s to %s." % \
#(def_lang_fieldname, fieldname,
#obj.__dict__[fieldname])
if not getattr(obj, def_lang_fieldname):
setattr(obj, def_lang_fieldname,
obj.__dict__[fieldname])
obj.save()
for fieldname in trans_opts.fields:
def_lang_fieldname =\
build_localized_fieldname(fieldname, DEFAULT_LANGUAGE)
# We'll only update fields which do not have an existing value:
model.objects.filter(Q(**{def_lang_fieldname: None}) |\
Q(**{def_lang_fieldname: ""})).update(\
**{def_lang_fieldname: F(fieldname)})