Skip to content

Instantly share code, notes, and snippets.

@mloza

mloza/migration.py

Created Apr 15, 2020
Embed
What would you like to do?
Kod do postu o dodawaniu nowej, unikalnej kolumny do istniejącej tabeli w Django znajdujący się pod adresem https://blog.mloza.pl/dodanie-unikalnej-kolumny-do-istniejacej-tabeli-w-django/‎
from __future__ import unicode_literals
import common.functions
from django.db import migrations, models
def uniquify_keys_for_events(apps, schema_editor):
events = apps.get_model("event", "Event").objects.all()
for event in events:
event.key = common.functions.generate_random_string_32()
event.save()
class Migration(migrations.Migration):
dependencies = [
('event', '0002_auto_20170102_1816'),
]
operations = [
migrations.AddField( # krok 1
model_name='event',
name='key',
field=models.CharField(max_length=32, null=True),
),
migrations.RunPython(uniquify_keys_for_events), # krok 2
migrations.AlterField( # krok 3
model_name='event',
name='key',
field=models.CharField(max_length=32, default=common.functions.generate_random_string_32, unique=True))
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.