mirror of
https://github.com/Hopiu/django-modeltranslation.git
synced 2026-05-11 06:43:10 +00:00
- Fixed an undefined name bug in add_localized_fields error message when a model already has a field by the name that is added. - Fixed redefined method name in ModeltranslationWithFileFields test class.
25 lines
1.1 KiB
Python
25 lines
1.1 KiB
Python
# -*- coding: utf-8 -*-
|
|
from django.db.models import F, Q
|
|
from django.core.management.base import NoArgsCommand
|
|
|
|
from modeltranslation.settings import DEFAULT_LANGUAGE
|
|
from modeltranslation.translator import translator
|
|
from modeltranslation.utils import build_localized_fieldname
|
|
|
|
|
|
class Command(NoArgsCommand):
|
|
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 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)})
|