Add app_label & model_name arguments to only update specific apps/models
@@ -11,12 +11,40 @@ class Command(BaseCommand):
help = ('Updates empty values of default translation fields using'
' values from original fields (in all translated models).')
+ def add_arguments(self, parser):
+ parser.add_argument(
+ 'app_label', nargs='?',
+ help='App label of an application to update empty values.',
+ )
+ parser.add_argument(
+ 'model_name', nargs='?',
+ help='Model name to update empty values of only this model.',
+ )
+
def handle(self, *args, **options):
verbosity = options['verbosity']
if verbosity > 0:
self.stdout.write("Using default language: %s" % DEFAULT_LANGUAGE)
models = translator.get_registered_models(abstract=False)
+
+ # optionally filter by given app_label
+ app_label = options['app_label']
+ if app_label:
+ models = [m for m in models if m._meta.app_label == app_label]
+
+ # optionally filter by given model_name
+ model_name = options['model_name']
+ if model_name:
+ model_name = model_name.lower()
+ models = [m for m in models if m._meta.model_name == model_name]
+
+ if verbosity > 0:
+ self.stdout.write("Working on models: %s" % ', '.join([
+ "{app_label}.{object_name}".format(**m._meta.__dict__)
+ for m in models
+ ]))
+
for model in models:
if verbosity > 0:
self.stdout.write("Updating data of model '%s'" % model)
