Skip to content

Instantly share code, notes, and snippets.

@mlncn
Created August 15, 2015 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlncn/68ca8b0dec7b3236ec13 to your computer and use it in GitHub Desktop.
Save mlncn/68ca8b0dec7b3236ec13 to your computer and use it in GitHub Desktop.
Worst dedupe function ever, but example of manual migration runnnig a function
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations
def dedupe_codes(apps, schema_editor):
codes = []
count = 0
measures = apps.get_model('measures', 'Measure')
for measure in measures.objects.all():
if measure.code in codes:
count += 1
measure.code = "deduped-{}-{}".format(count, measure.code)
measure.save()
codes.append(measure.code)
class Migration(migrations.Migration):
dependencies = [
('measures', '0025_measure_aggregation'),
]
operations = [
migrations.RunPython(dedupe_codes),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment