From 4ffbb9404f3033c3bb8a81bac7911ae22a79398b Mon Sep 17 00:00:00 2001 From: Luciano Almeida Date: Mon, 28 Mar 2016 16:51:23 -0300 Subject: [PATCH] =?UTF-8?q?Ajusta=20migra=C3=A7=C3=A3o=20para=20novas=20mu?= =?UTF-8?q?dan=C3=A7as=20das=20models?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luciano Almeida --- legacy/migration.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/legacy/migration.py b/legacy/migration.py index d98b17b19..34c6d2a34 100644 --- a/legacy/migration.py +++ b/legacy/migration.py @@ -93,7 +93,6 @@ def warn(msg): def get_fk_related(field, value, label=None): fields_dict = {} - if value is None and field.null is False: value = 0 if value is not None: @@ -169,7 +168,6 @@ def make_stub(model, id): class DataMigrator: - def __init__(self): self.field_renames, self.model_renames = get_renames() @@ -179,6 +177,8 @@ class DataMigrator: for field in new._meta.fields: old_field_name = renames.get(field.name) field_type = field.get_internal_type() + msg = ("Field %s (%s) from model %s " % + (field.name, field_type, field.model.__name__)) if old_field_name: old_value = getattr(old, old_field_name) if isinstance(field, models.ForeignKey): @@ -191,13 +191,23 @@ class DataMigrator: value = get_fk_related(field, old_value, label) else: value = getattr(old, old_field_name) + if (field_type == 'DateField' and + field.null is False and value is None): + names = [old_fields.name for old_fields + in old._meta.get_fields()] + combined_names = "(" + ")|(".join(names) + ")" + matches = re.search('(ano_\w+)', combined_names) + if not matches: + warn(msg + '=> setting 0000-01-01 value to DateField') + value = '0001-01-01' + else: + value = '%d-01-01' % getattr(old, matches.group(0)) + warn(msg + "=> settig %s for not null DateField" % + (value)) if field_type == 'CharField' or field_type == 'TextField': if value is None: - warn( - "Field %s (%s) from model %s" - " => settig empty string '' for %s value" % - (field.name, field_type, field.model.__name__, - value)) + warn(msg + "=> settig empty string '' for %s value" % + (value)) value = '' setattr(new, field.name, value)