Browse Source

Corrige conversao de nulo para vazio em char fields e text fields

pull/237/head
Eduardo Calil 9 years ago
parent
commit
5699266581
  1. 30
      scripts/convert_null_to_empty_in_all_char_fields.py

30
scripts/convert_null_to_empty_in_all_char_fields.py

@ -1,18 +1,22 @@
from django.db import models 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(): def convert_null_to_empty():
for app in sapl_appconfs: for name in SAPL_APPS:
for model in app.get_models(): for model in apps.get_app_config(name).get_models():
print('Convertendo null p/ vazio. model [%s]' try:
% model._meta.model_name) print('Convertendo null p/ vazio. model [%s]'
char_fields = [f for f in model._meta.fields % model._meta.model_name)
if isinstance(f, (models.CharField, char_fields = [f for f in model._meta.fields
models.TextField))] if isinstance(f, (models.CharField,
for obj in model.objects.all(): models.TextField))]
for field in char_fields: for obj in model.objects.all():
if getattr(obj, field.name) is None: for field in char_fields:
setattr(obj, field.name, '') if getattr(obj, field.name) is None:
obj.save() setattr(obj, field.name, '')
obj.save()
except Exception as e:
print(e)

Loading…
Cancel
Save