diff --git a/scripts/convert_null_to_empty_in_all_char_fields.py b/scripts/convert_null_to_empty_in_all_char_fields.py index 6f21f885e..66d896d5c 100644 --- a/scripts/convert_null_to_empty_in_all_char_fields.py +++ b/scripts/convert_null_to_empty_in_all_char_fields.py @@ -1,18 +1,22 @@ from django.db import models -from sapl.utils import sapl_appconfs +from django.apps import apps +from sapl.settings import SAPL_APPS def convert_null_to_empty(): - for app in sapl_appconfs: - for model in app.get_models(): - print('Convertendo null p/ vazio. model [%s]' - % model._meta.model_name) - char_fields = [f for f in model._meta.fields - if isinstance(f, (models.CharField, - models.TextField))] - for obj in model.objects.all(): - for field in char_fields: - if getattr(obj, field.name) is None: - setattr(obj, field.name, '') - obj.save() + for name in SAPL_APPS: + for model in apps.get_app_config(name).get_models(): + try: + print('Convertendo null p/ vazio. model [%s]' + % model._meta.model_name) + char_fields = [f for f in model._meta.fields + if isinstance(f, (models.CharField, + models.TextField))] + for obj in model.objects.all(): + for field in char_fields: + if getattr(obj, field.name) is None: + setattr(obj, field.name, '') + obj.save() + except Exception as e: + print(e)