Fix remove_null_from_text_fields migration

This commit is contained in:
Carl van Tonder 2018-11-20 12:59:28 -05:00
parent 1256d0f52b
commit f962a7e0fa

View File

@ -5,8 +5,6 @@ from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
import multiselectfield.db.fields import multiselectfield.db.fields
from apps.map.models import CaseStudy
string_fields = [ string_fields = [
'additional_technical_details', 'additional_technical_details',
'associated_infrastructure', 'associated_infrastructure',
@ -58,13 +56,17 @@ string_fields = [
def remove_nulls(apps, schema_editor): def remove_nulls(apps, schema_editor):
# We can't import the Person model directly as it may be a newer # We can't import the Person model directly as it may be a newer
# version than this migration expects. We use the historical version. # version than this migration expects. We use the historical version.
study = apps.get_model('map', 'CaseStudy') CaseStudy = apps.get_model('map', 'CaseStudy')
for study in CaseStudy.objects.all(): for study in CaseStudy.objects.all():
for field in string_fields: for field in string_fields:
if getattr(study, field) == None: if getattr(study, field) == None:
setattr(study, field, '') setattr(study, field, '')
try:
study.save() study.save()
except:
from pdb import set_trace; set_trace()
class Migration(migrations.Migration): class Migration(migrations.Migration):
@ -74,5 +76,5 @@ class Migration(migrations.Migration):
] ]
operations = [ operations = [
migrations.RunPython(remove_nulls), migrations.RunPython(remove_nulls, migrations.RunPython.noop),
] ]