Skip to content

Instantly share code, notes, and snippets.

@mloza
Created April 15, 2020 09:05
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 mloza/63737f7c04e929fef0830c8fcf27fb52 to your computer and use it in GitHub Desktop.
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/‎
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