def test_get_translatable_models(self):
        '''
        get_translatable_models() Should only work if django-modeltranslation is
        available.
        So if this test fails in your dev environment, you probably have
        django-modeltranslation installed
        '''

        with self.assertRaises(ImproperlyConfigured):
            get_translatable_models()
Esempio n. 2
0
    def handle(self, *args, **options):
        from modeltrans.migration import (
            I18nDataMigration,
            get_translatable_models,
            get_translated_fields,
        )

        models = get_translatable_models()

        apps = defaultdict(list)
        for model in models:
            apps[model._meta.app_label].append(model)

        for app in options["apps"]:
            print("Create data migration for app:", app)

            if len(apps[app]) == 0:
                print(
                    "No models registered for translation with django-modeltranslation"
                )
                break
            migration = I18nDataMigration(app)

            for Model in apps[app]:
                translatable_fields = tuple(get_translated_fields(Model))

                print('added model "{}" with fields [{}]'.format(
                    Model.__name__, str(translatable_fields)))
                migration.add_model(Model, translatable_fields)

            filename = migration.write_migration_file()
            print("Wrote migration for {} to {}".format(app, filename))
    def handle(self, *args, **options):
        from modeltrans.migration import (I18nMigration,
                                          get_next_migration_filename,
                                          get_translatable_models,
                                          get_translated_fields)

        models = get_translatable_models()

        apps = defaultdict(list)
        for model in models:
            apps[model._meta.app_label].append(model)

        for app in options['apps']:
            print('Create migration for app:', app)

            if len(apps[app]) == 0:
                print(
                    'No models registered for translation with django-modeltranslation'
                )
                return
            migration = I18nMigration(app)

            for Model in apps[app]:
                translatable_fields = tuple(get_translated_fields(Model))

                print('added model "{}" with fields [{}]('.format(
                    Model.__name__, str(translatable_fields)))
                migration.add_model(Model, translatable_fields)

            with open(get_next_migration_filename(app), 'w') as f:
                migration.write(f)