Created
April 15, 2020 09:05
-
-
Save mloza/63737f7c04e929fef0830c8fcf27fb52 to your computer and use it in GitHub Desktop.
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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